I want to retrieve a total for the same column but where each alias contains a different string? How do I do this, my sub query below fails.
SELECT status (
SELECT COUNT(status) FROM `table`
WHERE `status` LIKE LOWER('%stage 1%')
) AS Stage1,
status (
SELECT COUNT(status) FROM `table`
WHERE `status` LIKE LOWER('%stage 1%')
) AS Stage2,
status (
SELECT COUNT(status) FROM `table`
WHERE `status` LIKE LOWER('%stage 1%')
) AS Stage3
FROM `table`
答案 0 :(得分:2)
如果您想要三列,请使用条件聚合。这在MySQL中特别容易:
select sum(status LIKE LOWER('%stage 1%')) as status1,
sum(status LIKE LOWER('%stage 2%')) as status2,
sum(status LIKE LOWER('%stage 3%')) as status3
from `table`;
答案 1 :(得分:1)
Count the result of a CASE
statement that aggregates your groups. Here's a fiddle.
SELECT
CASE
WHEN status LIKE '%stage 1%' THEN 'stage 1'
WHEN status LIKE '%stage 2%' THEN 'stage 2'
WHEN status LIKE '%stage 3%' THEN 'stage 3'
END as status_group,
COUNT(*)
FROM statuses
GROUP BY status_group