Mysql SQL选择多个类似项的计数
如果是一个计数条件,我知道如何让它工作。如何使其在多种条件下工作Mysql SQL选择多个类似项的计数,mysql,sql,Mysql,Sql,如果是一个计数条件,我知道如何让它工作。如何使其在多种条件下工作 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 lik
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'
您应该使用子选择:
SELECT
(SELECT count(*) form TableName.DeviceName where DeviceName like 'AR%' ) as DEVICE_Type_A,
(SELECT count(*) from TableName.DeviceName where DeviceName like 'R%' ) as DEVICE_Type_B,
(SELECT count(*) from TableName.DeviceName where DeviceName like 'P%' ) as DEVICE_Type_C,
(SELECT count(*) from TableName.DeviceName where DeviceName like 'AM%' ) as DEVICE_Type_D,
FROM DB.TableName TableName
WHERE TableName.DURATIONMIN > '180'
您应该使用子选择:
SELECT
(SELECT count(*) form TableName.DeviceName where DeviceName like 'AR%' ) as DEVICE_Type_A,
(SELECT count(*) from TableName.DeviceName where DeviceName like 'R%' ) as DEVICE_Type_B,
(SELECT count(*) from TableName.DeviceName where DeviceName like 'P%' ) as DEVICE_Type_C,
(SELECT count(*) from 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'
您应该使用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'
好吧,你的问题确实没有详细说明你想要实现什么?好吧,你的问题确实没有详细说明你想要实现什么?谢谢你的帮助,但我得到这个错误“System.Data.OleDb.OledBeException:表不存在。ORA-00942:表或视图不存在于”我不知道你的数据库模式,在此错误中,您没有表(例如输入错误)。我将使用您帮助我的代码,再次感谢。感谢您的帮助,但我收到此错误“System.Data.OleDb.OledBeException:表不存在。ORA-00942:表或视图不存在于”我不知道您的数据库架构,在此错误中,您没有表(例如输入错误)。我将使用您帮助我的代码,再次感谢。非常感谢,您是救命恩人,这很有效,我有一个问题,为什么它在和括号中有效,而不放tablename。column)如果您可以回答,则不必。当这些是一个表时,别名是可选的。我只是删除了它以使SQL更具可读性。实际上,您应该包括表别名,所以我承认这是一种糟糕的做法。但是,使别名与名称相同是多余的。取而代之的是,使用一些简单的缩写,比如“tn”,它会让人联想到“TableName”,但要短得多。谢谢,我会把它放回去,因为我的表总容量超过20 DB,超过1000 DB。+1这种方法通常是最有效的。对于一个非常大的表,我们正在计算非常少的行数。。。使用合适的索引(例如,在
(DeviceName,DurationMin)
上,在WHERE
子句中添加一个谓词,和(DeviceName类似于'a%'或DeviceName类似于'P%'或DeviceName类似于'R%')
(从技术上讲,这些被称为大小写“表达式”)非常感谢,伙计,你是救命恩人,这很有效,我有个问题,为什么它在总和括号中有效,而不放tablename.column)如果你能回答,就不必了。当这是一个表时,别名是可选的。我只是删除了它以使SQL更具可读性。实际上,您应该包括表别名,所以我承认这是一种糟糕的做法。但是,使别名与名称相同是多余的。取而代之的是,使用一些简单的缩写,比如“tn”,它会让人联想到“TableName”,但要短得多。谢谢,我会把它放回去,因为我的表总容量超过20 DB,超过1000 DB。+1这种方法通常是最有效的。对于一个非常大的表,我们正在计算非常少的行数。。。使用合适的索引(例如,在(DeviceName,DurationMin)
上,在WHERE
子句中添加一个谓词,和(DeviceName类似于'a%'或DeviceName类似于'P%'或DeviceName类似于'R%')
(从技术上讲,这些被称为大小写“表达式”)