简体   繁体   中英

MySQL,PHP: count results per condition + get the total result that satisfies the conditions

querytest table:

  1. xxx | yyy
  2. 999 | 888
  3. 999 | 787
  4. 111 | 222

query:

SELECT *,
       SUM(CASE WHEN XXX LIKE '999' AND YYY RLIKE '[0-9]8[0-9]' THEN 1 ELSE 0 END) AS Count999,
       SUM(CASE WHEN XXX LIKE '111' AND YYY RLIKE '[0-9]2[0-9]' THEN 1 ELSE 0 END) AS Count111
    FROM querytest
    WHERE 
    ( XXX LIKE '999' AND YYY RLIKE '[0-9]8[0-9]') 
    OR 
    ( XXX LIKE '111' AND YYY RLIKE '[0-9]2[0-9]')

result:

Array
(
    [0] => 999
    [xxx] => 999
    [1] => 888
    [yyy] => 888
    [2] => 2
    [Count999] => 2
    [3] => 1
    [Count111] => 1
)

clarification:

I'm using php.and i want a query that

  • let me know how many results I can get from ( XXX LIKE '999' AND YYY RLIKE '[0-9]88[0-9]') and how many results i can get from ( XXX LIKE '111' AND YYY RLIKE '[0-9]23[0-9]') and so on. like in the previous query.

  • gets me all the results like if im using this query:

 SELECT * FROM querytest WHERE ( XXX LIKE '999' AND YYY RLIKE '[0-9]8[0-9]') OR ( XXX LIKE '111' AND YYY RLIKE '[0-9]2[0-9]') 

my question

how can I combine the two previous points into one query? plus the ability of using LIMIT and ORDER with the second point.

您或者需要进行单独的查询,或者使用UNION。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM