我有这样的查询
SELECT tb_post.*,
(SELECT GROUP_CONCAT(DISTINCT tb_category.category_name)
FROM tb_category
LEFT JOIN tb_terms ON tb_terms.parent_1 = tb_category.category_id
WHERE tb_terms.post_id = `tb_post`.post_id and category_type="post"
GROUP BY tb_terms.post_id) AS parent1
FROM (`tb_post`)
但这不在哪里。这总是返回表中的所有行..我想添加为什么?
我不想更改形状查询,我我需要来自tb_post但tb_category.category_type = $ value 的所有数据,并且不要更改查询的结构
答案 0 :(得分:1)
如果你不需要tb_post。*然后删除它,因为它调用数据库中的所有数据,如果内部查询得到更少的记录,那么tb.post然后它也显示最大记录(tb_post)
答案 1 :(得分:0)
使用INNER JOIN:
SELECT tb_post.*, tc.categories
FROM tb_post
INNER JOIN (
SELECT tb_terms.post_id, GROUP_CONCAT(DISTINCT tb_category.category_name) AS categories
FROM tb_category
LEFT JOIN tb_terms ON tb_terms.parent_1 = tb_category.category_id
WHERE tb_category.category_type="post"
GROUP BY tb_terms.post_id
) tc ON tc.post_id = tb_post.post_id
答案 2 :(得分:0)
最好的解决方案是修改你的查询,如@Rimas建议的那样,但是使用该函数的片段来生成该查询,我看不到足够的信息来看看如何做到这一点。
如果那是不可能的,那么我会试图滥用HAVING条款并做这样的事情: -
SELECT tb_post.*,
(SELECT GROUP_CONCAT(DISTINCT tb_category.category_name)
FROM tb_category
INNER JOIN tb_terms ON tb_terms.parent_1 = tb_category.category_id
WHERE tb_terms.post_id = `tb_post`.post_id
AND category_type="post"
GROUP BY tb_terms.post_id) AS parent1
FROM (`tb_post`)
HAVING parent1 IS NOT NULL