请帮助。 我有三张桌子。 我想选择用户未回答的问题 这是我的数据库结构
问题表:
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
希望很清楚。
答案 0 :(得分:-1)
以下内容应该可以工作,但是可以肯定的是,我需要您编辑问题并为每个表添加架构(创建表语句),以及一些应该产生的示例数据结果。
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尚未回答的问题添加了另一个答案。
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 |