我有一个表名是tb1
tb1
id zone pressure
1 India Yes
2 USA No
3 UK Yes
4 India Yes
5 AUS No
6 UK Yes
压力有两种类型的条目,如“是”和“否”,我需要这样的结果
Zone Pressure
Yes No
India 2 0
USA 0 1
UK 1 1
AUS 0 1
到目前为止我的努力......
$result = mysql_query("SELECT Zone FROM Tb1 WHERE Pressure = 'Yes'");
$num_rows = mysql_num_rows($result); echo $num_rows . " \n";
答案 0 :(得分:1)
SELECT zone,
SUM(pressure = 'Yes') AS `Yes`,
SUM(pressure = 'No') AS `No`
FROM tb1
GROUP BY zone
你需要的技巧。这是一个聚合查询。这是一个小提琴。 http://sqlfiddle.com/#!2/54889/2/0
答案 1 :(得分:1)
SELECT zone
, SUM(pressure='yes') yes
, SUM(pressure='no') no
FROM my_table
GROUP
BY zone;
答案 2 :(得分:1)
SELECT Zone,
SUM(pressure='Yes') as `Yes`,
SUM(pressure='No') as `No`
FROM Tb1
GROUP BY Zone;
答案 3 :(得分:0)
你可以这样做
select tb1.zone,
sum(tb1.pressure = 'Yes') as Yes,
sum(tb1.pressure = 'No') as No
from tb1
group by tb1.zone