总计4个别名

时间:2019-03-04 01:07:24

标签: 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 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

我想要另一列“总计”,其中包含所有4个别名的总和。

3 个答案:

答案 0 :(得分:3)

您可以尝试:

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

答案 1 :(得分:2)

由于您不能在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'

请注意,这假设对于任何给定的行,s1s2s3s4中只有一个等于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

这样做可以消除第一个查询要求仅s1s2s3s4中的一个等于fel的问题对于任何给定的行。

答案 2 :(得分:2)

您不能重复使用别名。编写代码的最简单方法是使用data() { showLitebox: false, }, methods: { showLitebox() { ... } }

in

实际上,整个查询可以简化为:

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';

MySQL在数字上下文中将布尔值视为数字,其中“ 1”为true,“ 0”为false。当您要计算某个表达式的真值数量时,这很方便。