mysql—同一表并集后两个值求和

odopli94  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(399)

我的问题是:

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

我的输出是:

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

cgyqldqp

cgyqldqp1#

如果只需要通过“location\u 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
kmbjn2e3

kmbjn2e32#

不用了 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

相关问题