gpt4 book ai didi

mysql - SQL select count for more than one like

转载 作者:行者123 更新时间:2023-12-05 08:28:27 27 4
gpt4 key购买 nike

如果它是单计数条件,我知道如何让它工作。如何让它适用于多种条件?

SELECT count(TableName.DeviceName where DeviceName like 'AR%' ) as DEVICE_Type_A,
count(TableName.DeviceName where DeviceName like 'R%' ) as DEVICE_Type_B,
count(TableName.DeviceName where DeviceName like 'P%' ) as DEVICE_Type_C,
count(TableName.DeviceName where DeviceName like 'AM%' ) as DEVICE_Type_D,
FROM DB.TableName TableName
WHERE TableName.DURATIONMIN > '180'

最佳答案

你应该使用 case 语句!

SELECT count(case when DeviceName like 'AR%' then 1 end) as DEVICE_Type_A,
count(case when DeviceName like 'R%' then 1 end) as DEVICE_Type_B,
count(case when DeviceName like 'P%' then 1 end) as DEVICE_Type_C,
count(case when DeviceName like 'AM%' then 1 end) as DEVICE_Type_D
FROM DB.TableName TableName
WHERE TableName.DURATIONMIN > '180'

我把计数留在里面了。我个人认为“总和”更清楚:

SELECT sum(case when DeviceName like 'AR%' then 1 else 0 end) as DEVICE_Type_A,
sum(case when DeviceName like 'R%' then 1 else 0 end) as DEVICE_Type_B,
sum(case when DeviceName like 'P%' then 1 else 0 end) as DEVICE_Type_C,
sum(case when DeviceName like 'AM%' then 1 else 0 end) as DEVICE_Type_D
FROM DB.TableName TableName
WHERE TableName.DURATIONMIN > '180'

关于mysql - SQL select count for more than one like,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11400946/

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