Mysql 合计4个别名
我有以下疑问:Mysql 合计4个别名,mysql,sql,Mysql,Sql,我有以下疑问: SELECT SUM(case when s1 = 'fel' then 1 else 0 end) as s1_count, SUM(case when s2 = 'fel' then 1 else 0 end) as s2_count, SUM(case when s3 = 'fel' then 1 else 0 end) as s3_count, SUM(case when s4 = 'fel' then 1 else 0 end) as s
SELECT
SUM(case when s1 = 'fel' then 1 else 0 end) as s1_count,
SUM(case when s2 = 'fel' then 1 else 0 end) as s2_count,
SUM(case when s3 = 'fel' then 1 else 0 end) as s3_count,
SUM(case when s4 = 'fel' then 1 else 0 end) as s4_count
FROM `arrest`
WHERE date BETWEEN '2018-12-01' AND '2019-03-01'
哪些产出:
s1_count s2_count s3_count s4_count
17 13 6 3
我想要另一个名为“total”的列,它包含所有4个别名的总和。由于您不能在
SELECT
子句中使用列别名,因此有几种其他方法可以做到这一点。向查询中添加另一个条件聚合:
SELECT
SUM(case when s1 = 'fel' then 1 else 0 end) as s1_count,
SUM(case when s2 = 'fel' then 1 else 0 end) as s2_count,
SUM(case when s3 = 'fel' then 1 else 0 end) as s3_count,
SUM(case when s4 = 'fel' then 1 else 0 end) as s4_count,
SUM(case when s1 = 'fel' or s2 = 'fel' or s3 = 'fel' or s4 = 'fel' then 1 else 0 end) as total
FROM `arrest`
WHERE date BETWEEN '2018-12-01' AND '2019-03-01'
注意,对于任何给定行,这假设只有s1
、s2
、s3
或s4
中的一个将等于fel
更可靠的方法是将原始查询用作子查询:
SELECT *, s1_count+s2_count+s3_count+s4_count AS total
FROM (SELECT
SUM(case when s1 = 'fel' then 1 else 0 end) as s1_count,
SUM(case when s2 = 'fel' then 1 else 0 end) as s2_count,
SUM(case when s3 = 'fel' then 1 else 0 end) as s3_count,
SUM(case when s4 = 'fel' then 1 else 0 end) as s4_count
FROM `arrest`
WHERE date BETWEEN '2018-12-01' AND '2019-03-01') a
请注意,由于MySQL将数值上下文中的布尔表达式视为1(true)或0(false),因此可以按如下方式简化这些查询:
SELECT
SUM(s1 = 'fel') as s1_count,
SUM(s2 = 'fel') as s2_count,
SUM(s3 = 'fel') as s3_count,
SUM(s4 = 'fel') as s4_count,
SUM((s1 = 'fel') + (s2 = 'fel') + (s3 = 'fel') + (s4 = 'fel')) as total
FROM `arrest`
WHERE date BETWEEN '2018-12-01' AND '2019-03-01'
SELECT *, s1_count+s2_count+s3_count+s4_count AS total
FROM (SELECT
SUM(s1 = 'fel') as s1_count,
SUM(s2 = 'fel') as s2_count,
SUM(s3 = 'fel') as s3_count,
SUM(s4 = 'fel') as s4_count
FROM `arrest`
WHERE date BETWEEN '2018-12-01' AND '2019-03-01') a
这样做就消除了第一个查询要求任何给定行的
s1
、s2
、s3
或s4
中只有一个等于fel
的问题。因为在SELECT
子句中不能使用列别名,所以有几种其他方法可以做到这一点。向查询中添加另一个条件聚合:
SELECT
SUM(case when s1 = 'fel' then 1 else 0 end) as s1_count,
SUM(case when s2 = 'fel' then 1 else 0 end) as s2_count,
SUM(case when s3 = 'fel' then 1 else 0 end) as s3_count,
SUM(case when s4 = 'fel' then 1 else 0 end) as s4_count,
SUM(case when s1 = 'fel' or s2 = 'fel' or s3 = 'fel' or s4 = 'fel' then 1 else 0 end) as total
FROM `arrest`
WHERE date BETWEEN '2018-12-01' AND '2019-03-01'
注意,对于任何给定行,这假设只有s1
、s2
、s3
或s4
中的一个将等于fel
更可靠的方法是将原始查询用作子查询:
SELECT *, s1_count+s2_count+s3_count+s4_count AS total
FROM (SELECT
SUM(case when s1 = 'fel' then 1 else 0 end) as s1_count,
SUM(case when s2 = 'fel' then 1 else 0 end) as s2_count,
SUM(case when s3 = 'fel' then 1 else 0 end) as s3_count,
SUM(case when s4 = 'fel' then 1 else 0 end) as s4_count
FROM `arrest`
WHERE date BETWEEN '2018-12-01' AND '2019-03-01') a
请注意,由于MySQL将数值上下文中的布尔表达式视为1(true)或0(false),因此可以按如下方式简化这些查询:
SELECT
SUM(s1 = 'fel') as s1_count,
SUM(s2 = 'fel') as s2_count,
SUM(s3 = 'fel') as s3_count,
SUM(s4 = 'fel') as s4_count,
SUM((s1 = 'fel') + (s2 = 'fel') + (s3 = 'fel') + (s4 = 'fel')) as total
FROM `arrest`
WHERE date BETWEEN '2018-12-01' AND '2019-03-01'
SELECT *, s1_count+s2_count+s3_count+s4_count AS total
FROM (SELECT
SUM(s1 = 'fel') as s1_count,
SUM(s2 = 'fel') as s2_count,
SUM(s3 = 'fel') as s3_count,
SUM(s4 = 'fel') as s4_count
FROM `arrest`
WHERE date BETWEEN '2018-12-01' AND '2019-03-01') a
这样做可以消除第一个查询的问题,即对于任何给定行,只有s1
、s2
、s3
或s4
中的一个将等于fel
。您可以尝试:
SELECT s1_count, s2_count, s3_count, s4_count,
SUM(s1_count + s2_count + s3_count + s4_count) as total_count
FROM
(
SELECT
SUM(case when s1 = 'fel' then 1 else 0 end) as s1_count,
SUM(case when s2 = 'fel' then 1 else 0 end) as s2_count,
SUM(case when s3 = 'fel' then 1 else 0 end) as s3_count,
SUM(case when s4 = 'fel' then 1 else 0 end) as s4_count
FROM `arrest`
WHERE date BETWEEN '2018-12-01' AND '2019-03-01'
) q
您可以尝试:
SELECT s1_count, s2_count, s3_count, s4_count,
SUM(s1_count + s2_count + s3_count + s4_count) as total_count
FROM
(
SELECT
SUM(case when s1 = 'fel' then 1 else 0 end) as s1_count,
SUM(case when s2 = 'fel' then 1 else 0 end) as s2_count,
SUM(case when s3 = 'fel' then 1 else 0 end) as s3_count,
SUM(case when s4 = 'fel' then 1 else 0 end) as s4_count
FROM `arrest`
WHERE date BETWEEN '2018-12-01' AND '2019-03-01'
) q
不能重复使用别名。编写代码的最简单方法是在中使用
:
SELECT SUM(case when s1 = 'fel' then 1 else 0 end) as s1_count,
SUM(case when s2 = 'fel' then 1 else 0 end) as s2_count,
SUM(case when s3 = 'fel' then 1 else 0 end) as s3_count,
SUM(case when s4 = 'fel' then 1 else 0 end) as s4_count,
SUM(case when 'fel' in (s1, s2, s3, s4 ) then 1 else 0 end) as total
FROM `arrest`
WHERE date BETWEEN '2018-12-01' AND '2019-03-01';
实际上,整个查询可以简化为:
SELECT SUM( s1 = 'fel' ) as s1_count,
SUM( s2 = 'fel' ) as s2_count,
SUM( s3 = 'fel' ) as s3_count,
SUM( s4 = 'fel' ) as s4_count,
SUM( 'fel' in (s1, s2, s3, s4 ) ) as total
FROM `arrest`
WHERE date BETWEEN '2018-12-01' AND '2019-03-01';
MySQL将布尔值视为数字上下文中的数字,其中“1”表示true,“0”表示false。当您想要计算某个表达式的真值时,这非常方便。您不能重复使用别名。编写代码的最简单方法是在
中使用:
SELECT SUM(case when s1 = 'fel' then 1 else 0 end) as s1_count,
SUM(case when s2 = 'fel' then 1 else 0 end) as s2_count,
SUM(case when s3 = 'fel' then 1 else 0 end) as s3_count,
SUM(case when s4 = 'fel' then 1 else 0 end) as s4_count,
SUM(case when 'fel' in (s1, s2, s3, s4 ) then 1 else 0 end) as total
FROM `arrest`
WHERE date BETWEEN '2018-12-01' AND '2019-03-01';
实际上,整个查询可以简化为:
SELECT SUM( s1 = 'fel' ) as s1_count,
SUM( s2 = 'fel' ) as s2_count,
SUM( s3 = 'fel' ) as s3_count,
SUM( s4 = 'fel' ) as s4_count,
SUM( 'fel' in (s1, s2, s3, s4 ) ) as total
FROM `arrest`
WHERE date BETWEEN '2018-12-01' AND '2019-03-01';
MySQL将布尔值视为数字上下文中的数字,其中“1”表示true,“0”表示false。当您想计算某个表达式的真值时,这很方便。在MySQL 5.7+、MySQL 8.0+中,此查询不起作用。在MySQL 5.7+、MySQL 8.0+中,此查询不起作用。