简体   繁体   English

MySQL,PHP:按条件计算结果+获得满足条件的总结果

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

querytest table: querytest表:

  1. xxx | xxx | yyy y
  2. 999 | 999 | 888 888
  3. 999 | 999 | 787 787
  4. 111 | 111 | 222 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 我正在使用php。并且我想要一个查询

  • 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. 让我知道我可以从(XXX LIKE'999'和YYY RLIKE'[0-9] 88 [0-9]')得到多少结果,我可以从(XXX LIKE'111'和YYY RLIKE '[0-9] 23 [0-9]'),依此类推。 like in the previous query. 就像上一个查询一样。

  • gets me all the results like if im using this query: 让我获得所有结果,例如im使用此查询:

 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. 加上第二点使用LIMIT和ORDER的功能。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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