【问题标题】:Sum two values after Union of same table在同一张表的 Union 之后对两个值求和
【发布时间】:2019-02-15 16:45:34
【问题描述】:

这是我的查询:

SELECT location_description as Crimes,COUNT(*)
FROM test_sample
where test_sample.day_of_week LIKE"%sunday"
GROUP BY test_sample.location_description
UNION ALL
SELECT location_description as Crimes,COUNT(*)
FROM test_sample
where test_sample.day_of_week LIKE"%saturday"
GROUP BY test_sample.location_description

我的输出是:

如何将这两个重复值合并为一个。

【问题讨论】:

    标签: mysql sql sum union


    【解决方案1】:

    如果您只需要通过 'location_description' 字段获取记录数,那么您可以只使用没有联合的分组

    SELECT location_description AS Crimes,
           COUNT(*)
    FROM test_sample
    WHERE day_of_week LIKE "%sunday"
      OR day_of_week LIKE "%saturday"
    GROUP BY location_description
    

    【讨论】:

    • 你打字比我快!但是您忘记添加解释。无论如何 +1。
    • 谢谢!我被添加了小解释
    • 非常感谢..我不知道我让我的问题更复杂..谢谢:)
    【解决方案2】:

    现在不需要UNION,只需调整WHERE 以包含这两天;

    SELECT location_description as Crimes,COUNT(*)
    FROM test_sample
    where test_sample.day_of_week LIKE"%sunday"
       or test_sample.day_of_week LIKE"%saturday"
    GROUP BY test_sample.location_description
    

    【讨论】:

    • 非常感谢..我不知道我让我的问题更复杂..谢谢:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-07
    • 1970-01-01
    • 1970-01-01
    • 2015-12-30
    • 2019-11-21
    • 1970-01-01
    相关资源
    最近更新 更多