内连接有三个表

时间:2017-05-18 22:25:38

标签: php mysql

我的数据库中有三个表:评论,地标,用户,我希望从每个表中获取一些属性,我尝试使用以下查询,但没有重新启动,这是我的3个表

user (userid,firstName,lastName)
comment (userid,L_ID,review)
landmarks (L_ID,Title)

用户和对userid的评论 和L_ID

上的地标评论
  $q = mysqli_query($con,"SELECT l.*,c.id,c.review,CONCAT(u.firstName,' ',u.lastName) name FROM landmarks l inner join comment c on l.L_ID on c.L_ID inner join users u on u.userid=c.userid WHERE ".(isset($userid) ? "c.userid=$userid" : (isset($landmarkid) ? "c.L_ID=$landmarkid" : "") )." ORDER BY c.id");

1 个答案:

答案 0 :(得分:1)

我相信你的"内部联接评论c"

的ON部分有错误
SELECT l.*,c.id,c.review,CONCAT(u.firstName,' ',u.lastName) name 
FROM landmarks l 
inner join comment c 
    on l.L_ID 
    on c.L_ID 
inner join users u 
    on u.userid=c.userid 
WHERE ".(isset($userid) ? "c.userid=$userid" : (isset($landmarkid) ? "c.L_ID=$landmarkid" : "") )." 
ORDER BY c.id

我相信你想要的是

SELECT l.*,c.id,c.review,CONCAT(u.firstName,' ',u.lastName) name 
FROM landmarks l 
inner join comment c 
    on c.L_ID = l.L_ID 
inner join users u 
    on u.userid=c.userid 
WHERE ".(isset($userid) ? "c.userid=$userid" : (isset($landmarkid) ? "c.L_ID=$landmarkid" : "") )." 
ORDER BY c.id