0

Hi,

I have problem to count a multiple condition in one query. Usually i have to select count it one by one just like this :

SELECT COUNT(kod_urusan) as num1
FROM customer_info
WHERE kod_urusan = '0001'

SELECT COUNT(kod_urusan) as num2
FROM customer_info
WHERE kod_urusan = '0002'

SELECT COUNT(kod_urusan) as num3
FROM customer_info
WHERE kod_urusan = '0003'

Is there any way to put all this query in one query? So the result of the count should be in 3 column.

1
Contributor
1
Reply
7
Views
1 Week
Discussion Span
Last Post by Haji Sameon
0

Sorry for posting this newbie question. I just got the answer from some other site. This is the query i use it to solve this problem.

SELECT
    COUNT(CASE WHEN `kod_urusan` = '0001' THEN 1 END) AS count1,
    COUNT(CASE WHEN `kod_urusan` = '0002' THEN 1 END) AS count2,
    COUNT(CASE WHEN `kod_urusan` = '0003' THEN 1 END) AS count3
FROM customer_info;     

Hope this would help others who have the same problem like me :D

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.