php - 三表内联

标签 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");

最佳答案

我认为您的“内部连接评论 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

关于php - 三表内联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44058719/

相关文章:

php - 使用 PHP 从 HP ALM 导出所有缺陷

php - 合并具有相同数量项目的 2 个数组 - php

php - 如何在 php 中添加延迟显示数据库值

mysql - 按顺序编号

php - 在文件/etc/php5/cli/php.ini 中进行更改时启用 PHP 错误不适用

php - Codeigniter:在模型与 Controller 、模型与模型、 Controller 与 Controller 之间使用变量的最简单方法

php - 如何通过 PHP 将网站中的整个数字替换为波斯数字?

php - Codeigniter MySQL 查询

c# - 如何在 C# 中覆盖转储文件中的特定字节

Mysql 随机排序特色产品,但按日期对正常产品进行排序