首先,当我扩展第一个查询以解决第二个任务时,我被要求找到喜欢蓝眼睛的人的人,这很容易,然后我被要求找到至少两个喜欢绿眼睛的人的人。我的查询没有任何结果。
该查询在2个表(人员和点赞)上运行 人员包含人员,personB_id ==>人员)
如果有人可以帮助我编写第二个查询,将不胜感激:)
在下面,您可以找到我分别为第一个任务和第二个任务编写的查询。
任务1:
SELECT distinct p1.name
FROM Persons as p1,Persons as p2, Likes as l
where p1.id =l.personA_id
And p2.id = l.personB_id
And p2.eyeColor= "blue"
任务2:
SELECT distinct p1.name
FROM Persons as p1,Persons as p2, Likes as l, Persons as p3
where p1.id =l.personA_id
And p2.id = l.personB_id
And p3.id = l.personB_id
And p2.id = l.personB_id <> p3.id = l.personB_id
And p2.eyeColor= "green"
And p3.eyeColor= "green"
答案 0 :(得分:0)
我正在考虑“分组依据”:
SELECT pa.name
FROM Likes l JOIN
Persons pa
ON pa.id = l.personA_id JOIN
Persons pb
ON pb.id = l.personB_id
WHERE pb.eyeColor = 'green'
GROUP BY pa.name, pa.id
HAVING COUNT(*) >= 2;
注意:
FROM
子句中使用逗号。这是21世纪,这种语法已经过了二十多年了。JOIN
语法。GROUP BY
包含人员ID,以防万一两个人的姓名相同。