开发者

Mysql query with group_concat in subquery not giving correct results

I have 2 tables:

  • category(inter_archi_cat table) and their linking with other
  • 开发者_高级运维entities(inter_archi table )

I want to select only categories which linked to any entity.

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). But while running with main query its giving only 1 record.


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


You don't have to use IN and subquery. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜