具有多项选择的 MySQL 测验和用户选择未回答的问题

标签 mysql sql

请帮忙。 我有三张 table 。 我想选择用户没有回答的问题 这是我的数据库结构

问题表:

id, q_title, first_choice, second_choice, third_choice, right_answer

答案表:

 id, q_id, u_id, the_answer, isAnswered

用户表:

    id, email, password, name, phone

我尝试了很多 SQL 语句,但没有达到我想要的效果。 这是我的尝试之一

SELECT questions.id,questions.q_title,answers.q_id,answers.u_id,users.id
FROM questions,
     answers,
     users
WHERE questions.id not in (SELECT answers.q_id from answers
                           WHERE users.id = answers.u_id)
  and users.id = 3

问题表的示例数据:

 id, q_title, first_choice, second_choice, third_choice, right_answer
 1      q1       f1               f2          f3            f2
 2      q2       f1               f2          f3            f1
 3      q3       f1               f2          f3            f3

答案表的示例数据:

 id, q_id, u_id, the_answer, isAnswered
 1    1     3       f2        true
 2    3     2       f3        true

用户表的示例数据:

 id,      email,      password, name,   phone
  1    user@xxx.com     xp       x1     21564
  2    user2@xxx.com    xp       x2     56841
  3    user3@xxx.com    xp       x3     95682

现在 ID 为 3 的用户回答了问题一。 并且有三个问题我想要 id 为 3 的用户未回答的问题。

预期结果:

id,   q_title,  q_id,  u_id, id
1      q2        2      3    3
2      q3        3      3    3

希望它是清楚的。

最佳答案

以下应该有效,但可以肯定的是,我需要您编辑您的问题并为每个表添加架构(create table 语句),以及一些应该生成的示例数据结果。

SELECT 
    q1.id,
    q1.q_title,
    a1.q_id,
    u.id
FROM users u
LEFT JOIN answers a
    ON a.u_id = u.id
JOIN questions q
    ON a.q_id = q.id
LEFT JOIN answers a1
    ON a1.u_id <> u.id
JOIN questions q1
    ON a1.q_id = q1.id
WHERE a.id IS NULL
    AND (NOT a1.id IS NULL)
    AND u.id = 8
GROUP BY q1.id,
    q1.q_title,
    a1.q_id,
    u.id

更新:

我对您的 SQL Fiddle 进行了一些研究,并提出了一个解决方案。我为用户 8 没有回答的问题添加了另一个答案。

SQL Fiddle

MySQL 5.6 架构设置:

CREATE TABLE `answers` (
  `id` int(11) NOT NULL,
  `q_id` int(11) NOT NULL,
  `u_id` int(11) NOT NULL,
  `the_answer` text CHARACTER SET utf8 NOT NULL,
  `isAnswered` text CHARACTER SET utf8 NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

--
-- Dumping data for table `answers`
--

INSERT INTO `answers` (`id`, `q_id`, `u_id`, `the_answer`, `isAnswered`) VALUES
(1, 1, 2, 'first_choice', 'true'),
(4, 1, 0, 'second_choice', 'true'),
(2, 7, 8, 'third_choice', 'true'),
(3, 9, 8, 'third_choice', 'true');


CREATE TABLE `questions` (
  `id` int(11) NOT NULL,
  `q_title` text NOT NULL,
  `first_choice` text NOT NULL,
  `second_choice` text NOT NULL,
  `third_choice` text NOT NULL,
  `right_answer` text NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

--
-- Dumping data for table `questions`
--

INSERT INTO `questions` (`id`, `q_title`, `first_choice`, `second_choice`, `third_choice`, `right_answer`) VALUES
(9, 'ما هي عاصمة فلسطين؟', 'القدس', 'بغداد', 'مكة', 'القدس'),
(10, 'ما هي عاصمة انجلترا؟', 'واشنطن', 'كييف', 'لندن', 'لندن'),
(1, 'ما هي سنة استغلال السودان؟', '1965', '1956', '1989', '1956'),
(7, 'ما هي عاصمة اليونان؟', 'اثينا', 'كييف', 'واشنطن', 'اثينا'),
(8, 'ما هي عاصمة السعودية؟', 'الرياض', 'عمان', 'القاهرة', 'الرياض');


CREATE TABLE `users` (
  `id` int(11) NOT NULL,
  `email` varchar(200) NOT NULL,
  `password` mediumtext NOT NULL,
  `name` mediumtext NOT NULL,
  `phone` varchar(15) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `users` (`id`, `email`, `password`, `name`, `phone`) VALUES
(3, 'ahmed.aau5@gmail.com', '$2y$10$AC8q.PFdZNWN23OqwKbryOoy/zkutJEtSSS0VXR8qUXaQuWflAle6', 'Ahmed Mohamed', '249117446667'),
(2, 'ahmed.aau4@gmail.com', '$2y$10$kM2RD8nDNdsegk5km5bAyOqatWj9F45YdaElxXUq9uiywa.zgwgO6', 'Ahmed Mohamed', '249901222620'),
(4, 'ahmed.juvg@vhjv.com', '$2y$10$oqnnymFr/BTTMUkYWAc9LuQ4RhsrBMohPOPlTFs5FiLJecJan2Cl.', 'Ahmed', '249117446667'),
(8, 'ahmed@gmail.com', '$2y$10$epia0tc3V4m1wlU4sqva7eV4fGq4ekzh2ZDb.tRP6VSA4WPxytyiG', 'Ahmed', '249901222620');

查询 1:

SELECT
    q1.id,
    q1.q_title,
    a1.id as `ans_id`,
    a1.the_answer,
    u1.id
FROM questions q1
LEFT JOIN
  (SELECT 
      q.id as questionid
  FROM users u
  LEFT JOIN answers a
      ON a.u_id = u.id  AND a.isAnswered = 'true'
  LEFT JOIN questions q
      ON a.q_id = q.id
  WHERE u.id = 8
  GROUP BY q.id) x1
  ON x1.questionid = q1.id
JOIN answers a1
  ON q1.id = a1.q_id
LEFT JOIN users u1
  ON u1.id = 8
WHERE x1.questionid IS NULL
GROUP BY
    q1.id,
    q1.q_title,
    a1.id,
    a1.the_answer,
    u1.id

Results :

用户 8 没有回答的问题。

| id |                              q_title | ans_id |    the_answer | id |
|----|-------------------------------|--------|---------------|----|
|  1 | ما هي سنة استغلال السودان؟ |      1 |  first_choice |  8 |
|  1 | ما هي سنة استغلال السودان؟ |      4 | second_choice |  8 |

关于具有多项选择的 MySQL 测验和用户选择未回答的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53053055/

相关文章:

java - 使用java从mysql备份特定表

sql - IBM DB2 : BIGINT to VARCHAR 中的数据类型转换

php - 如何获取MySQL数据库中的总行数?

sql - tsql 字符串与 select 和 order by 的连接不能与 order by 子句中的函数一起使用?

mysql - 是否可以使用动态外键,最好/正确的做法是什么?

php - 保持表示 (HTML) 和逻辑 (PHP) 分离

mysql - 如何在mysql中获取下一个自动增量ID

php - 如何在 Laravel 中使用 PDO 进行 block 更新

mysql - 我应该如何构建这个数据库?

sql - 来自 PostgreSQL 函数的多个 JDBC 结果集