Php 仅对一个选定值应用where子句
假设我有一张如下所示的桌子:Php 仅对一个选定值应用where子句,php,mysql,sql,Php,Mysql,Sql,假设我有一张如下所示的桌子: id quantity 1 5 2 3 3 6 sql="select quantity where id='2' sum(quantity) as total_quantity from table"; 我想检索id=2的行的数量和表的总量,我想我可以做如下操作: id quantity 1 5 2 3 3 6 sql="select quantity where id=
id quantity
1 5
2 3
3 6
sql="select quantity where id='2' sum(quantity) as total_quantity from table";
我想检索id=2的行的数量和表的总量,我想我可以做如下操作:
id quantity
1 5
2 3
3 6
sql="select quantity where id='2' sum(quantity) as total_quantity from table";
现在,我知道这个说法是错误的,但我希望你能理解我在这里的意图。如何实现这样的目标?使用条件聚合:
select sum(case when id = 2 then quantity end) as qty2,
sum(quantity) as total_quantity
from t;
使用工会:
SELECT quantity FROM table WHERE id = 2
UNION SELECT SUM(quantity) FROM table