开发者

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开发者_Python百科' 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.


You either need to make separate queries, or use UNION.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜