我有 3 个包含以下数据的表:
第一个表称为连接,其中connections.username1
一个connections.username2
是跟随者,也是被跟随者。
它有以下几行:
connections.username1 | connections.username2
mikha | guy
guy | maricela
maricela | guy
第二个表称为问题。它有一列用于提问者questions.asker_username
,另一列用于接收问题的人questions.target_username
。当提问者被称为“sys.tem”而目标被称为“every.one”时,它被认为是一个全球性的问题,所有成员都可以回答。
匿名用户可以询问,他们的 ip 被记录为asker_username
.
它有以下几行:
questions.id | questions.asker_username | questions.target_username | questions.question
1 | mikha | guy | what's your name?
2 | mikha | maricela | What's your age?
3 | guy | mikha | what's your name?
4 | maricela | guy | favorite food?
5 | xx.xx.xxx.xx | mikha | favorite pet?
6 | xx.xx.xxx.xx | guy | first name?
7 | xx.xx.xxx.xx | maricela | first name?
8 | sys.tem | every.one | what's ur name?
9 | sys.tem | every.one | favorite movie?
10 | sys.tem | every.one | favorite game?
第三张表称为答案。答案表中的 id 与问题 id 相同。此表有一列用于 id 和 username 和 answer。
answers.id | answers.username | answers.answer
1 | guy | my name is guy
2 | maricela | my name is maricela
3 | mikha | my name is mikha
4 | guy | pizza
8 | guy | guy is my name
8 | maricela | maricela is my name
9 | maricela | avatar
我想要一个结合以下与“mikha”相关的条件和他关注的人的查询:
1)questions.asker_username
不是“米哈”
2) questions.target_username
要么是“mikha”,要么是他关注的任何用户。
3)如果questions.target_username
等于“every.one”并由“mikha”回答,则显示问题。
4) 如果questions.target_username
等于“every.one”并且由“mikha”关注的任何人回答,则显示问题及其答案。如果“mikha”关注的用户没有回答,请不要显示问题。
5) 如果questions.target_username
等于“every.one”并且根本没有被任何人回答,则显示问题一次。
6) 如果questions.target_username
等于“every.one”并且没有被“mikha”回答并且没有被他关注的任何人回答,则只显示一次问题。
我使用以下查询:
SELECT questions.id,answers.id,questions.asker_username,questions.target_username,
answers.username,questions.question,answers.answer
FROM questions
LEFT JOIN answers ON (questions.id = answers.id)
LEFT JOIN connections ON connections.username1 = 'mikha'
AND (questions.target_username = connections.username2
OR questions.asker_username = connections.username2
OR connections.username2 = answers.username)
WHERE questions.asker_username <> 'mikha'
AND (questions.target_username = 'mikha'
OR questions.target_username = connections.username2
OR (questions.target_username = 'every.one'
AND (answers.username = 'mikha'
OR answers.username = connections.username2
OR answers.username IS NULL)
)
)
GROUP BY questions.id,answers.username
我期望的结果:
questions.id | answers.id | questions.asker_username | questions.target_username | answers.username | questions.question | answers.answer
3 | 3 | guy | mikha | mikha | what's your name? | my name is mikha
4 | 4 | maricela | guy | guy | favorite food? | pizza
5 | 5 | xx.xx.xxx.xx | mikha | NULL | favorite pet? | NULL
6 | 6 | xx.xx.xxx.xx | guy | NULL | first name? | NULL
8 | 8 | sys.tem | every.one | NULL | what's ur name? | NULL
8 | 8 | sys.tem | every.one | guy | what's ur name? | guy is my name
9 | 9 | sys.tem | every.one | NULL | favorite movie? | NULL
10 | 10 | sys.tem | every.one | NULL | favorite game? | NULL
我实际得到的结果:
questions.id | answers.id | questions.asker_username | questions.target_username | answers.username | questions.question | answers.answer
3 | 3 | guy | mikha | mikha | what's your name? | my name is mikha
4 | 4 | maricela | guy | guy | favorite food? | pizza
5 | 5 | xx.xx.xxx.xx | mikha | NULL | favorite pet? | NULL
6 | 6 | xx.xx.xxx.xx | guy | NULL | first name? | NULL
8 | 8 | sys.tem | every.one | guy | what's ur name? | guy is my name
10 | 10 | sys.tem | every.one | NULL | favorite game? | NULL
我在http://sqlfiddle.com/#!2/29929e/1上构建了一个方案,向您展示我实际得到的结果
谢谢 :)