我有这个问题:
SELECT category_name, category_id, category_slug
FROM categories
WHERE EXISTS (
SELECT 1
FROM assigned_categories
WHERE assigned_categories.assigned_category_id = categories.category_id
)
我想获得另一个名为'visible'
TABLE
的{{1}}字段的值,所以看起来像这样:
'posts'
我尝试了很多东西,但我认为我做错了什么。有什么帮助吗?
答案 0 :(得分:0)
不知道,如果我理解你的问题,但听起来你想要做一个内部联接? 尝试这样的事情:
SELECT category_name, category_id, category_slug
FROM categories
inner join ft_projects on ft_projects.category_id = categories.id
WHERE EXISTS (
SELECT 1
FROM assigned_categories
WHERE assigned_categories.assigned_category_id = categories.category_id
)
and ft_projects.visible = 1
当然你必须调整你的连接条件,“on ft_projects.category_id = categories.id”只是一个例子。
同样重要的是:我认为你想要用你的存在条件实现什么,也可以通过内连接更好地解决(至少我假设这样,基于你的列名)。
答案 1 :(得分:0)
好的弄明白,这就是诀窍:
SELECT category_name, category_id, category_slug
FROM categories
WHERE EXISTS (
SELECT 1, visible
FROM assigned_categories
JOIN posts ON assigned_categories.assigned_post_id = post_id
WHERE assigned_categories.assigned_category_id = ft_categories.category_id AND visible = 1
)