gpt4 book ai didi

mysql - 合并多个相似查询

转载 作者:行者123 更新时间:2023-11-29 07:36:25 25 4
gpt4 key购买 nike

我可以从多个简单的查询中获得结果,但我想将它们进一步合并为子查询。

所有子查询都将与此类似-

SELECT COUNT(count) AS acc1 FROM (SELECT COUNT(table.colname) AS count 
FROM tablename GROUP BY tablename.sumcol HAVING count=1) as access1

其他人也是

SELECT COUNT(count) AS acc2 FROM (SELECT COUNT(table.colname) AS count
FROM tablename GROUP BY tablename.sumcol HAVING count=2) as access2

最佳答案

您可以使用 UNION ALL 在同一结果集中获得两个结果

  SELECT 'acc1' , COUNT(count) 
FROM ( SELECT COUNT(table.colname) AS count
FROM tablename G
ROUP BY tablename.sumcol
HAVING count=1) access1

UNION ALL

SELECT 'acc2', COUNT(count)
FROM (SELECT COUNT(table.colname) AS count
FROM tablename
GROUP BY tablename.sumcol
HAVING count=2) access2

我添加了 'acc1' 和 'acc2' 文字值以获得更好的结果读取,但你可以避免它

如果您需要同一行上的两个结果,您可以使用交叉连接(笛卡尔积)

  SELECT COUNT(count) as count_acc1, T.count2
FROM ( SELECT COUNT(table.colname) AS count1
FROM tablename G
GROUP BY tablename.sumcol
HAVING count=1) access1

CROSS JOIN (

SELECT COUNT(count) as count2
FROM (SELECT COUNT(table.colname) AS count
FROM tablename
GROUP BY tablename.sumcol
HAVING count=2) access2 ) T

关于mysql - 合并多个相似查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48560443/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com