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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.