[英]Mysql query with group_concat in subquery not giving correct results
I have 2 tables:我有 2 张桌子:
inter_archi_cat
table) and their linking with otherinter_archi_cat
表)及其与其他的链接inter_archi
table )inter_archi
表) I want to select only categories which linked to any entity.我只想 select 链接到任何实体的类别。
SELECT *
FROM inter_archi_cat
WHERE id IN (SELECT GROUP_CONCAT(DISTINCT sub_cat) as allcat
FROM inter_archi)
If I ran the subquery individually, I'm giving correct results (apx 40 records).如果我单独运行子查询,我会给出正确的结果(apx 40 记录)。 But while running with main query its giving only 1 record.
但是在使用主查询运行时,它只给出 1 条记录。
I have done it:我已经做了:
here is changed query:这是更改的查询:
SELECT distinct cat.id,cat.name
FROM inter_archi_cat as cat
join inter_archi as inter on (cat.id in (inter.sub_cat))
got idea from从那里得到想法
MySQL - How to use subquery into IN statement by value MySQL - 如何在 IN 语句中按值使用子查询
You don't have to use IN and subquery.您不必使用 IN 和子查询。 You can achieve the same result using simple inner join:
您可以使用简单的内部连接来获得相同的结果:
SELECT ic.* FROM inter_archi_cat ic
JOIN inter_archi i
ON i.sub_cat = ic.id
GROUP BY ic.id
which would be a lot faster.这会快很多。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.