gpt4 book ai didi

mysql - 如何改进包含子查询的 SELECT 语句?

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

我做了这个选择,但我认为它可以改进。有谁知道如何是让它变得更好的最佳方法?

 SELECT  SUBSTRING_INDEX(acctEmail, '@', -1) as Domain, 
(SELECT count(*) FROM accounts
WHERE acctType = 'A' AND SUBSTRING_INDEX(acctEmail, '@', -1) = Domain
GROUP BY SUBSTRING_INDEX(acctEmail, '@', -1)
) as qtdAlias,
(SELECT count(*) FROM accounts
WHERE acctType = 'C' AND SUBSTRING_INDEX(acctEmail, '@', -1) = Domain
GROUP BY SUBSTRING_INDEX(acctEmail, '@', -1)
) as qtdContas,
(SELECT count(*) FROM accounts
WHERE acctType = 'L' AND SUBSTRING_INDEX(acctEmail, '@', -1) = Domain
GROUP BY SUBSTRING_INDEX(acctEmail, '@', -1)
) as qtdListas,
count(*) as Total
FROM accounts
GROUP BY Domain
ORDER BY Total DESC;

它带来了以下结果:

Result

最佳答案

一种方法是将条件总和设为

select
substring_index(acctEmail, '@', -1) as Domain,
sum(
case when acctType = 'A' then 1 else 0 end
) as qtdAlias,
sum(
case when acctType = 'C' then 1 else 0 end
) as qtdContas,
sum(
case when acctType = 'L' then 1 else 0 end
) as qtdListas,
count(*) as Total
from accounts
GROUP BY Domain
ORDER BY Total DESC;

关于mysql - 如何改进包含子查询的 SELECT 语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29517142/

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