我正在撞墙,试图弄清楚这一个。 我有一个2路左连接查询工作完美:
'SELECT f.user_id, u.avatar
FROM following AS f
LEFT JOIN users AS u ON u.username = f.user_id
WHERE f.follower_id = "'.$u.'"';
从一个像桌子这样的推特,然后从一个单独的成员表中添加他们的头像,从而拉出一个成员列表。
现在我正在尝试从第三个表(实际的推文)中提取所有数据,但它不起作用:
'SELECT f.user_id, u.avatar, p.*
FROM following AS f
LEFT JOIN users AS u ON u.username = f.user_id
WHERE f.follower_id = "'.$u.'"
LEFT JOIN posts AS p ON p.username = u.username';
我也尝试过添加:
WHERE p.username = "'.$u.'"';
到最后,但它并没有什么区别。该错误只是说第二个左连接存在SQL语法错误。
关于我缺少什么的任何建议?
答案 0 :(得分:2)
join
是from
子句的一部分。 where
子句跟在from
:
SELECT f.user_id, u.avatar, p.*
FROM following AS f
LEFT JOIN users AS u ON u.username = f.user_id
LEFT JOIN posts AS p ON p.username = u.username
WHERE f.follower_id = "'.$u.'" ;
答案 1 :(得分:0)
你需要在加入之后使用where条件:
'SELECT f.user_id, u.avatar, p.*
FROM following AS f
LEFT JOIN users AS u ON u.username = f.user_id
LEFT JOIN posts AS p ON p.username = u.username WHERE f.follower_id = "'.$u.'" ' ;
答案 2 :(得分:0)
将WHERE子句移动到最后一次LEFT JOIN之后。
SELECT f.user_id, u.avatar, p.*
FROM following AS f
LEFT JOIN users AS u ON u.username = f.user_id
LEFT JOIN posts AS p ON p.username = u.username
WHERE f.follower_id = "'.$u.'";
另外,出于安全原因,您可能希望使用预准备语句,而不是将$ u直接连接到查询。