感谢您对我的糟糕问题的反馈。第一次在这里。
我正在Visual Studio中使用SQlite和C#。
这是我表中数据的示例:
TABLE: VIDEOS
-------------
ID VideoName VideoStarID VideoCostarID
--------------------------------------------------------------
1 Casablanca 23 43
2 Maltese Falcon 23 156
3 Matrix 234 312
4 Matrix Reloaded 234 312
5 Matrix Revolutions 234 312
6 Pompeii 312
TABLE: STARS
-------------
ID StarName
------------------------------
23 Humphrey Bogart
43 Ingrid Bergman
156 Mary Astor
234 Keanu Reeves
312 Carrie-Anne Moss
TABLE: TAGS
-------------
ID TagName
------------------------------
1 Action
2 Classic
3 Drama
4 Horror
5 Thriller
6 Sci-Fi
7 Romance
8 Western
9 War
10 Film Noir
11 Comedy
12 History
13 Dystopia
TABLE: VIDEO_TAGS
-------------
VideoID TagID
------------------------------
1 2
1 5
1 7
1 9
2 1
2 2
2 5
3 1
3 5
3 6
3 13
4 1
4 5
4 6
4 13
5 1
5 5
5 6
5 13
6 12
6 3
我想选择所有由Carrie-Anne Moss和Keanu Reeves主演的,带有“动作”,“科幻”和“惊悚”标签的电影。实际上,我的查询将比将来的查询更为复杂,因为我将必须能够包括年份,工作室,奥斯卡提名,获胜奖,评级等参数;但目前,我遇到的问题是我无法获得如下输出:
-----------------------------------------------------------------------------------------------------
|VideoID | VideoName | StarName | CostarName | Tags |
-----------------------------------------------------------------------------------------------------
| 3 |Matrix | Keanu Reeves | Carrie-Anne Moss | Action, Sci-Fi, Thriller, Dystopia |
| 4 |Matrix Reloaded | Keanu Reeves | Carrie-Anne Moss | Action, Sci-Fi, Thriller, Dystopia |
| 4 |Matrix Revolutions | Keanu Reeves | Carrie-Anne Moss | Action, Sci-Fi, Thriller, Dystopia |
-----------------------------------------------------------------------------------------------------
我尝试了这个(显然)错误的查询:
SELECT VIDEOS.ID, VIDEOS.VideoName, VIDEOS.StarID, VIDEOS.CostarID, STARS.StarName, GROUP_CONCAT(TAGS.Name) AS Tags
FROM VIDEOS
INNER JOIN STARS ON VIDEOS.StarID = STARS.ID
INNER JOIN VIDEO_TAGS ON VIDEOS.ID = VIDEO_TAGS.VideoID
INNER JOIN TAGS ON TAGS.ID = VIDEO_TAGS.TagID
WHERE
VIDEOS.StarID = 234 AND
VIDEOS.CostarID = 312 AND
VIDEO_TAGS = 1 AND
VIDEO_TAGS = 5 AND
VIDEO_TAGS = 6
结果仅一行。它不能获取所有电影。我也没有得到想要的标签。
希望此信息有所帮助。任何帮助将不胜感激。
谢谢。
答案 0 :(得分:0)
您缺少的是此子查询:
select distinct vt.videoid from video_tags vt
where (select count(distinct tagid) from video_tags where videoid = vt.videoid and tagid in (1, 5, 6)) = 3
为您提供包含所有3个标签的视频的ID。
所以用这个:
select v.id VideoID, v.videoname VideoName, s1.starname StarName, s2.starname CoStarName,
(select group_concat(tagname) from tags where id in (select tagid from video_tags where videoid = v.id)) Tags
from videos v inner join (
select distinct vt.videoid from video_tags vt
where (select count(distinct tagid) from video_tags where videoid = vt.videoid and tagid in (1, 5, 6)) = 3
) f
on f.videoid = v.id
inner join stars s1
on s1.id = v.videostarid
left join stars s2
on s2.id = v.videocostarid
where s1.id = 234 and s2.id = 312
请参见demo
答案 1 :(得分:0)
由于缺少GROUP BY,因此只得到1行。 另外,您需要将IN用于3个标签。
SELECT VIDEOS.ID, VIDEOS.VideoName, VIDEOS.StarID, VIDEOS.CostarID, STARS.StarName, GROUP_CONCAT(TAGS.TagName) AS Tags
FROM VIDEOS
INNER JOIN STARS ON VIDEOS.StarID = STARS.ID
INNER JOIN VIDEO_TAGS ON VIDEOS.ID = VIDEO_TAGS.VideoID
INNER JOIN TAGS ON TAGS.ID = VIDEO_TAGS.TagID
WHERE
VIDEOS.StarID = 234 AND
VIDEOS.CostarID = 312 AND
VIDEO_TAGS.TagID IN (1,5,6)
GROUP BY VIDEOS.ID