1

my question similar like this one MySQL COUNT() multiple columns.

but for me , how do i count the mutliple row:

report table:

id     t_pro      t_price
1.    voucher    20.00
2.    pro        13.00
3.    pro 1x     13.00
4.    pro 2x       13.00
5.    voucher    20.00
6.    pro 1x       13.00 

$db->query("SELECT COUNT(*) FROM (SELECT t_pro WHERE t_pro LIKE 'pro%' AS pro_tan, SELECT t_pro WHERE t_pro LIKE 'voucher%' AS voucher) WHERE date=CURDATE()  ");

the result something like:

voucher : 2
pro tan : 4
1
  • why are you using a select within a select? Commented Apr 27, 2011 at 16:15

3 Answers 3

3
SELECT  CASE WHEN t_pro LIKE 'voucher%' THEN 'voucher' WHEN t_pro LIKE 'pro%' THEN 'pro tan' END AS pro,
        COUNT(*)
FROM    mytable
WHERE   t_pro LIKE 't_pro%' OR t_pro LIKE 'voucher%'
GROUP BY
        pro
Sign up to request clarification or add additional context in comments.

3 Comments

@Quassnoi: thanks. how do i echoing out the result? echo pro tan = $r['pro'] ??
im not using any framework. its custom code on my own. how do i echoing the result like voucher: 2, pro tan: 4
@Quassnoi: sorry my bad :D.. its from $db = new Database() class
2

You can use GROUP BY:

SELECT t_pro, COUNT(*) FROM report GROUP BY t_pro;

NOTE: this is if you want the count for each t_pro value. Otherwise, you'd need to modify the GROUP BY for custom t_pro value associations

Comments

0
select t_pro, count(1) from table group by t_pro;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.