在子查询中使用 group_concat 进行 MySQL 查询未给出正确的结果

发布于 2024-11-04 15:52:56 字数 364 浏览 0 评论 0原文

我有 2 个表:

  • 类别(inter_archi_cat 表)及其与其他
  • 实体的链接(inter_archi 表)

我只想选择链接到任何实体的类别。

SELECT * 
  FROM inter_archi_cat 
 WHERE id IN (SELECT GROUP_CONCAT(DISTINCT sub_cat) as allcat 
                FROM inter_archi)

如果我单独运行子查询,我将给出正确的结果(大约 40 条记录)。但在使用主查询运行时,它只给出 1 条记录。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挥剑断情 2024-11-11 15:52:56

我已经做到了:

这里是更改的查询:

SELECT distinct cat.id,cat.name
  FROM inter_archi_cat  as cat
  join   inter_archi as inter on (cat.id in (inter.sub_cat))

得到想法

MySQL - 如何按值在 IN 语句中使用子查询

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

回忆凄美了谁 2024-11-11 15:52:56

您不必使用 IN 和子查询。您可以使用简单的内部联接获得相同的结果:

SELECT ic.* FROM inter_archi_cat ic 
JOIN inter_archi i 
ON i.sub_cat = ic.id 
GROUP BY ic.id

这会快得多。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文