我有一个名为问题的表,其中包含以下行:
questions.id | questions.target_username
1 | every.one
2 | every.one
我还有以下行的答案表:
answers.id | answers.username
1 | guy
1 | maricela
2 | mikha
正如您所看到的,相同的问题ID可能会有来自不同用户的答案。 我想在以下情况下只选择行:
a)问题的答案不包括(mikha)为answers.username,在这种情况下只显示一次问题ID
b)问题的答案用户名(mikha)为answers.username。
我使用以下查询:
SELECT questions.id, answers.username
FROM questions
LEFT JOIN answers ON ( questions.id = answers.id )
WHERE questions.target_username = 'every.one'
AND (
answers.username IS NOT NULL
OR answers.username = 'mikha'
)
GROUP BY questions.id, answers.username
结果我期待:
questions.id | answers.usernme
1 |
2 | mikha
我真正得到的是:
questions.id | answers.usernme
1 | guy
1 | maricela
2 | mikha
提前致谢 问候 Micahel
答案 0 :(得分:3)
您只需要将answers.username = 'mikha'
条件移动到on子句,此时甚至可以删除条件中的冗余条件。
SELECT questions.id, answers.username
FROM questions
LEFT JOIN answers ON(questions.id = answers.id AND answers.username = 'mikha')
WHERE questions.target_username = 'every.one'
GROUP BY questions.id, answers.username;
编辑: 我不确定这是否是一项要求,但如果您想省略尚未有人回答的问题,以下是您想要的:
SELECT questions.id, answers.username
FROM questions
LEFT JOIN answers ON(questions.id = answers.id AND answers.username = 'mikha')
LEFT JOIN answers others ON(questions.id = others.id AND others.username <> 'mikha')
WHERE questions.target_username = 'every.one'
AND (
(
answers.id IS NOT NULL
AND others.id IS NULL
)
OR (
answers.id IS NULL
AND others.id IS NOT NULL
)
)
GROUP BY questions.id, answers.username;
编辑2:这是我在测试表中的内容,以及查询#2的结果:
SELECT * FROM questions;
+----+-----------------+
| id | target_username |
+----+-----------------+
| 1 | every.one |
| 2 | every.one |
| 3 | every.one |
| 4 | every.one |
+----+-----------------+
4 rows in set (0.00 sec)
SELECT * FROM answers;
+-----+----+----------+
| ida | id | username |
+-----+----+----------+
| 1 | 1 | guy |
| 2 | 1 | maricela |
| 3 | 2 | mikha |
+-----+----+----------+
3 rows in set (0.00 sec)
(Run query #2 above)
+----+----------+
| id | username |
+----+----------+
| 1 | NULL |
| 2 | mikha |
+----+----------+
2 rows in set (0.00 sec)
编辑3:以下是更新的表格数据和符合您在评论中添加的条件的更新查询:
SELECT * FROM questions;
+----+-----------------+
| id | target_username |
+----+-----------------+
| 1 | every.one |
| 2 | every.one |
| 3 | every.one |
| 4 | every.one |
+----+-----------------+
4 rows in set (0.00 sec)
SELECT * FROM answers;
+-----+----+----------+
| ida | id | username |
+-----+----+----------+
| 1 | 1 | guy |
| 2 | 1 | maricela |
| 3 | 2 | mikha |
| 7 | 4 | guy |
| 8 | 4 | mikha |
+-----+----+----------+
5 rows in set (0.00 sec)
新查询:
SELECT questions.id, answers.username
FROM questions
LEFT JOIN answers ON(questions.id = answers.id AND answers.username = 'mikha')
LEFT JOIN answers others ON(questions.id = others.id AND others.username <> 'mikha')
WHERE questions.target_username = 'every.one'
AND (
answers.username = 'mikha'
OR (
answers.id IS NULL
AND others.id IS NOT NULL
)
)
GROUP BY questions.id, answers.username;
结果:
+----+----------+
| id | username |
+----+----------+
| 1 | NULL |
| 2 | mikha |
| 4 | mikha |
+----+----------+
3 rows in set (0.00 sec)
答案 1 :(得分:0)
你必须改变答案表,
更改答案。想问题。我添加answer.Id作为主键...
在你的目标不明确,因为你期望的结果有第2行,mikha(你说如果用户名是mikha,我只需要问题ID)?
答案 2 :(得分:0)
如果你加入来回答id只有那些'mikha'的用户名你应该得到想要的结果。那些有答案而不是mikhas答案的id将获得null值。我不明白你在哪里需要问题表来获取这些信息:
SELECT answers.id, a2.username
FROM answers
LEFT JOIN (
SELECT id, username
FROM answers
WHERE username = 'mikha'
) AS a2 ON a2.id = answers.id
GROUP BY answers.id
答案 3 :(得分:0)
您获得此结果的原因是:
answers.username IS NOT NULL OR answers.username = 'mikha'
状况不佳。第二种情况answers.username = 'mikha'
已经是answers.username IS NOT NULL
的情况 - 所以这是一个毫无意义的第二个条件。LEFT JOIN
加入此条件时,基本上只相当于INNER JOIN
。