[英]Select 2 tables with one select query
I have 2 tables on my database. 我的数据库中有2个表。 One table has categories and the other one has posts. 一个表具有类别,另一个表具有职位。 In the category table I have a column called "active". 在类别表中,我有一列称为“活动”。
What I'm trying to do, is to select all posts which have category=0. 我想做的是选择所有类别= 0的帖子。
Here's the select that I got so far: 这是我到目前为止获得的选择:
SELECT tb_posts.*, tb_categories.active
FROM tb_posts, tb_categories
WHERE tb_categories.active='0'
ORDER BY datapub DESC
The above query doesn't work as it loads all posts not matter the category status. 上面的查询不起作用,因为无论类别状态如何,它都会加载所有帖子。 :( :(
You need a left join
: 您需要left join
:
SELECT tp.*, tc.active
FROM tb_posts tp
LEFT JOIN tb_categories tc ON tb.category_id = tc.id
WHERE tc.active='0'
ORDER BY datapub DESC
The join is done on the category
column (replace with proper column name - this was my guess) which is (probably) referencing the id
in the tb_categories
(again, replace the name). 联接是在category
列上完成的(用适当的列名替换-这是我的猜测),(可能)引用tb_categories
的id
(再次替换名称)。
For an exact query, please show your table structures. 对于确切的查询,请显示您的表结构。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.