2014-02-18 60 views
-1

我有一個表名PHP是TB1count行與特定值

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"; 
+4

你嘗試過什麼到目前爲止? – Rikesh

+0

$ result = mysql_query(「SELECT Zone FROM Tb1 WHERE'Pressure' ='Yes'」); $ num_rows = mysql_num_rows($ result); echo $ num_rows。 「\ n」; – user3283373

+0

但這是給所有區域中的所有總行不是特別區域類型 – user3283373

回答

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

0

你可以做

select tb1.zone, 
sum(tb1.pressure = 'Yes') as Yes, 
sum(tb1.pressure = 'No') as No 
from tb1 
group by tb1.zone 
1
SELECT zone 
    , SUM(pressure='yes') yes 
    , SUM(pressure='no') no 
    FROM my_table 
GROUP 
    BY zone; 
+0

!!! gazumped !!! – Strawberry

1
SELECT Zone, 
     SUM(pressure='Yes') as `Yes`, 
     SUM(pressure='No') as `No` 
FROM Tb1 
GROUP BY Zone;