我需要根据下面的查询返回两组数据,但我得到四组
SELECT DISTINCT *,
CASE WHEN likes.face_id = persona.face_id THEN '1' ELSE '0' END liked,
CASE WHEN dislikes.face_id = persona.face_id THEN '1' ELSE '0' END disliked,
CASE WHEN comments.face_id = persona.face_id THEN '1' ELSE '0' END commented
FROM persona, likes, dislikes, comments
RIGHT JOIN tagged ON tagged.phone_id = 'FA' WHERE persona.face_id = tagged.face_id
下面是我要查询的表格
标记表:
phone_id face_id likeCount dislikeCount commentCount FA GA 1 1 1 FA SA 1 0 0
角色表:
face_id name age GA Frank 34 SA Mark 24
喜欢桌子:
face_id phone_id GA FA SA FA
不喜欢表:
face_id phone_id SA FA
评论表:
face_id phone_id comment GA FA good
我从上面的查询得到四个值(四组数据)而不是两个(两组数据)值
以下数据集是在php
中回显时上述查询的结果"数据" : [{" face_id":" GA"" likeCount":" 1"" dislikeCount":&#34 1"" commentCount":" 1""名称":"马克""年龄" :" 34""喜欢":" 1""评价":" 1""讨厌的":" 1"},
{" face_id":" SA"" likeCount":" 1"" dislikeCount":& #34; 0"" commentCount":" 0""名称":"弗兰克""年龄&# 34;:" 24""喜欢":" 0""评价":" 0"&# 34;不喜欢":" 0"},
{" face_id":" GA"" likeCount":" 1"" dislikeCount":& #34; 1"" commentCount":" 1""名称":" GA""年龄&# 34;:" 34""喜欢":" 0""评价":" 1"&# 34;不喜欢":" 1"},
{" face_id":" SA"" likeCount":" 1"" dislikeCount":& #34; 0"" commentedCount":" 0""名称":"弗兰克""年龄&# 34;:" 24""喜欢":" 1""评价":" 0"&# 34;不喜欢":" 0"}]}
答案 0 :(得分:0)
更通用的方法
SELECT * ,
CASE WHEN likes.face_id = persona.face_id THEN '1' ELSE '0' END liked,
CASE WHEN dislikes.face_id = persona.face_id THEN '1' ELSE '0' END disliked,
CASE WHEN comments.face_id = tagged.face_id THEN '1' ELSE '0' END commented
FROM tagged JOIN persona ON persona.face_id = tagged.face_id
LEFT JOIN likes ON likes.face_id = persona.face_id AND likes.phone_id = 'FA'
LEFT JOIN dislikes ON dislikes.face_id = tagged.face_id AND dislikes.phone_id = 'FA'
LEFT JOIN comments ON comments.face_id = tagged.face_id AND comments.phone_id = 'FA'
WHERE tagged.phone_id = 'FA'
但是有一个问题,face_ids都是null