Lat*_*tox 0 php mysql sql aggregate-functions
我想今天选择所有信息作为变量,如下所示:
SELECT * FROM `table` WHERE `cat` = 'BA' and `date` LIKE '03/28%'
SELECT * FROM `table` WHERE `cat` = 'BB' and `date` LIKE '03/28%'
SELECT * FROM `table` WHERE `cat` = 'BC' and `date` LIKE '03/28%'
SELECT * FROM `table` WHERE `cat` = 'BD' and `date` LIKE '03/28%'
Run Code Online (Sandbox Code Playgroud)
但是我没有为每个人做一个新的查询,而是想提出:
$ba = the total number of results returned;
$bb = the total number of results returned;
$bc = the total number of results returned;
$bd = the total number of results returned;
Run Code Online (Sandbox Code Playgroud)
我该怎么做呢?
使用:
SELECT SUM(CASE WHEN t.cat = 'BA' THEN 1 ELSE 0 END) AS ba,
SUM(CASE WHEN t.cat = 'BB' THEN 1 ELSE 0 END) AS bb,
SUM(CASE WHEN t.cat = 'BC' THEN 1 ELSE 0 END) AS bc,
SUM(CASE WHEN t.cat = 'BD' THEN 1 ELSE 0 END) AS bd
FROM YOUR_TABLE t
WHERE t.date LIKE '03/28%'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
145 次 |
| 最近记录: |