gpt4 book ai didi

php - mySQL 查询,数据作为 php 变量?

转载 作者:行者123 更新时间:2023-12-01 00:31:27 28 4
gpt4 key购买 nike

我想选择今天的所有信息作为变量,如下所示:

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%'

但是我不想为每个查询都做一个新的查询,我只想提出:

$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;

我该怎么做?

最佳答案

使用:

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%'

关于php - mySQL 查询,数据作为 php 变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5454272/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com