[英]How should i edit WHERE clause in SQL query to get desired result?
Let's say i have two tables called "Videos" and "Reviews"假设我有两个名为“视频”和“评论”的表格
Videos:
int: video_id (PK)
String: video_name
.
.
Reviews:
int: review_id (PK)
int: video_id (FK)
String: review
.
.
In a scenario where i want to get all videos and their related reviews, i use the query:在我想获取所有视频及其相关评论的场景中,我使用查询:
"SELECT * FROM Videos v, Reviews r WHERE v.video_id = r.review_id"
But this query only returns the videos with a review, the videos without a review is not returned, but i also need them.但是这个查询只返回有评论的视频,没有评论的视频不返回,但我也需要它们。
How should i edit the query to see videos without a review too?我应该如何编辑查询以在没有评论的情况下观看视频?
Example result:示例结果:
(video_id), (video_name), (review_id), (review)
1, "videoName", 1, "review1"
2, "videoName2", NULL, NULL
Thank you for your answers and comments.感谢您的回答和评论。 (sorry for the typo)
(对不起打字错误)
Seems you have some typos there - Travels or Videos Nevertheless, here is the query based on your posted query:似乎您在那里有一些错别字-游记或视频不过,这是基于您发布的查询的查询:
SELECT * FROM Travels t left join Reviews r on t.travel_id = r.review_id
Just a left join will do the trick.只需一个左连接就可以解决问题。
You use a "Left Join":您使用“左连接”:
select v.video_id, v.video_name, r.review_id, r.review
from Videos v
left join Reviews r on v.video_id = r.video_id;
(Your sample code is using old style join and it should really be the new style changing LEFT to INNER in the sample code) (您的示例代码使用的是旧样式连接,它应该是示例代码中将 LEFT 更改为 INNER 的新样式)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.