LEFT JOIN之前的WHERE子句不起作用

时间:2014-12-03 23:55:15

标签: php sql mysqli

我的工作正常:

$sql = $mysqli->query("SELECT posts.*, members.username, categories.category_name
                       FROM posts
                       LEFT JOIN members
                            ON posts.post.by = members.id
                       LEFT JOIN categories
                            ON posts.post_category = categories.category_id
                       ORDER BY posts.post_date DESC
                       LIMIT $start, $limit
                      ");

但我想要这个WHERE条件:

WHERE posts.post_by = 1

我无法找到正确的方法,我已经尝试将它放在最后,在LIMIT之前,在FROM帖子之后,但结果是空的。

1 个答案:

答案 0 :(得分:2)

where子句位于from

之后
                   SELECT posts.*, members.username, categories.category_name
                   FROM posts
                   LEFT JOIN members
                        ON posts.post.by = members.id
                   LEFT JOIN categories
                        ON posts.post_category = categories.category_id
                   WHERE posts.post_by = 1
                   ORDER BY posts.post_date DESC
                   LIMIT $start, $limit;

joinfrom子句的一部分,这就是我将查询缩进为:

的原因
                   SELECT posts.*, members.username, categories.category_name
                   FROM posts LEFT JOIN
                        members
                        ON posts.post.by = members.id LEFT JOIN
                        categories
                        ON posts.post_category = categories.category_id
                   WHERE posts.post_by = 1
                   ORDER BY posts.post_date DESC
                   LIMIT $start, $limit;