简体   繁体   English

SQL:3表内部联接返回多个结果

[英]SQL: 3 Table Inner Join Returns Multiple Results

SELECT video.name, video.description, video_source.url, bitcast_user.username
FROM video
INNER JOIN bitcast_user ON video.account_id = bitcast_user.id
INNER JOIN video_source ON video_source.video_id = {$_GET['id']};

This returns results with incorrect video names, descriptions and associated accounts but correct sources. 这会返回带有错误视频名称,描述和相关帐户但来源正确的结果。 There is a one-to-many relationship between users and videos, and videos and sources. 用户和视频以及视频和源之间存在一对多的关系。

You shouldn't do an INNER JOIN on a variable - that belongs in the WHERE clause. 您不应该对属于WHERE子句的变量进行INNER JOIN。 (Actually - I was even surprised that this worked at all.) I think you need a query like this: (实际上-这让我感到非常惊讶。)我认为您需要这样的查询:

SELECT video.name, video.description, video_source.url, bitcast_user.username
  FROM video
  INNER JOIN bitcast_user ON video.account_id = bitcast_user.id
  INNER JOIN video_source ON video_source.video_id = video.video_id
  WHERE video_source.video_id = {$_GET['id']};

(I'm not sure about video_source.video_id = video.video_id because I don't know how the column is named in the video table.) (我不确定video_source.video_id = video.video_id因为我不知道该列在video表中的命名方式。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM