如何在MySQL中加入四个表[PHP]

时间:2014-05-23 00:21:30

标签: php mysql sql join

我有四张桌子,我想加入所有桌子。我已经成功加入了3个表,但是当我尝试加入第4个表时,它不起作用。我已将error_reporting(E_ALL);设置为or die(mysqli_error($con));并将votes添加到查询的末尾,但它不会显示任何错误,只会显示白屏。

当我试图加入投票表时,它停止了工作。

我的表格为users rings posts $sql = mysqli_query($con, "SELECT * FROM posts p INNER JOIN rings r ON p.rid = r.id INNER JOIN users u ON p.uid = u.id INNER JOIN votes v ON p.pid = v.pid WHERE p.rid IN ('$rja') AND p.uid != '$uid' AND p.deleted = '0' ORDER BY p.date_posted DESC" ) or die(mysqli_error($con));

这是我的问题:

$rja

vid | pid | uid | vote_type 是一个数组。

投票表:

id | username | password | email

用户表:

pid | uid | rid | body | votes | deleted | date_posted

帖子表:

id | title | category | rating | user_created

戒指表:

{{1}}

如果您有任何疑问,请发表评论

1 个答案:

答案 0 :(得分:3)

尝试左连接

 SELECT * FROM posts p 
   INNER JOIN rings r ON p.rid = r.id 
   INNER JOIN users u ON p.uid = u.id 
   LEFT JOIN votes v ON p.pid = v.pid 
   WHERE p.rid IN ('$rja') AND p.uid != '$uid' 
   AND p.deleted = '0' 
   ORDER BY p.date_posted DESC"