1

我有一个名为 questions 的表格,其中包含以下行:

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

提前致谢 问候米迦勒

4

4 回答 4

3

您只需要将answers.username = 'mikha'条件移至 on 子句,此时您甚至可以删除多余的 where 条件。

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)
于 2012-12-07T17:58:04.727 回答
0

你必须改变你的答案表,

将 answer.Id 更改为 question.Id 并添加 answer.Id 作为主键...

你的目标不清楚,因为你期望的结果有第 2 行,mikha(你说如果用户名是 mikha,我只需要问题 ID)?

于 2012-12-07T17:20:34.837 回答
0

如果您只加入回答 ids 那些“mikha”的用户名,您应该会得到想要的结果。那些有答案但没有 mikhas 答案的 id 将获得空值。我不明白您需要问题表在哪里获取此信息:

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
于 2012-12-07T17:29:41.657 回答
0

你得到这个结果的原因是:

  1. answers.username IS NOT NULL OR answers.username = 'mikha'是一个糟糕的状态。第二种情况,answers.username = 'mikha'已经是其中的一种情况answers.username IS NOT NULL——所以它是一个毫无意义的第二个条件。
  2. LEFT JOIN加上这个条件,本质上就等同于INNER JOIN
于 2012-12-07T17:42:29.817 回答