MySQL query and PHP: how to know how many results per each "WHERE" condition
im using php and I have this mysql query :
SELECT * FROM numbers
WHERE
( XXX LIKE '999' AND YYY RLIKE '[0-9]88[0-9]')
OR
( XXX LIKE '111' AND YYY RLIKE '[0-9]23[0-9]')
My Question: how can I know how many results I get from ( XXX LIKE '999' AND YYY RLIKE '[0-9]88[0-9]') and how many results i get from ( XXX LIKE '111' AND YYY RLIKE '[0-9]23[0-9]') and so on. I dont want to split them into two queries because i have many of these conditions in my query. plus i still want to get the result that开发者_StackOverflow satisfies the conditions. please help me
SELECT *,
SUM(CASE WHEN XXX LIKE '999' AND YYY RLIKE '[0-9]88[0-9]' THEN 1 ELSE 0 END) AS Count999,
SUM(CASE WHEN XXX LIKE '111' AND YYY RLIKE '[0-9]23[0-9]' THEN 1 ELSE 0 END) AS Count111
FROM numbers
WHERE
( XXX LIKE '999' AND YYY RLIKE '[0-9]88[0-9]')
OR
( XXX LIKE '111' AND YYY RLIKE '[0-9]23[0-9]')
I think it'd be something like:
SELECT
(SELECT COUNT(*) FROM numbers WHERE XXX LIKE '999' AND YYY RLIKE '[0-9]88[0-9]') AS 'CountA',
(SELECT COUNT(*) FROM numbers WHERE XXX LIKE '111' AND YYY RLIKE '[0-9]23[0-9]') AS 'CountB'
use the function mysql_num_rows.
精彩评论