在子查询中使用 group_concat 进行 MySQL 查询未给出正确的结果
我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我已经做到了:
这里是更改的查询:
得到想法
从MySQL - 如何按值在 IN 语句中使用子查询
I have done it:
here is changed query:
got idea from
MySQL - How to use subquery into IN statement by value
您不必使用 IN 和子查询。您可以使用简单的内部联接获得相同的结果:
这会快得多。
You don't have to use IN and subquery. You can achieve the same result using simple inner join:
which would be a lot faster.