0

I have a table name is tb1

tb1
id zone  pressure
1  India  Yes
2   USA   No
3   UK    Yes
4  India  Yes
5  AUS    No
6   UK    Yes

Pressure have two kind of entry like "yes" and "no" i need result in this manner

Zone   Pressure
        Yes  No
India    2   0
USA      0   1
UK       1   1
AUS      0   1

My effort so far...

$result = mysql_query("SELECT Zone FROM Tb1 WHERE Pressure = 'Yes'");
$num_rows = mysql_num_rows($result); echo $num_rows . " \n";
5
  • 4
    Have you tried anything so far ? Commented Feb 18, 2014 at 13:24
  • $result = mysql_query("SELECT Zone FROM Tb1 WHERE Pressure = 'Yes'"); $num_rows = mysql_num_rows($result); echo $num_rows . " \n"; Commented Feb 18, 2014 at 13:27
  • but this is giving all total row in all zone not in particular zone type Commented Feb 18, 2014 at 13:28
  • 1
    @user3283373 edit your answer rather than putting these in the comments. Commented Feb 18, 2014 at 13:31
  • Stop using mysql_query. And stop respecting whoever told you to use it in the first place; it's been deprecated for a while now. Look into mysqli or PDO. Commented Feb 18, 2014 at 13:33

4 Answers 4

1
SELECT zone,
       SUM(pressure = 'Yes') AS `Yes`,
       SUM(pressure = 'No') AS `No`
  FROM tb1
 GROUP BY zone

does the trick you need. It's an aggregate query. Here's a fiddle. http://sqlfiddle.com/#!2/54889/2/0

Sign up to request clarification or add additional context in comments.

Comments

1
SELECT zone
     , SUM(pressure='yes') yes
     , SUM(pressure='no') no 
  FROM my_table 
 GROUP 
    BY zone;

Comments

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

Comments

0

You can do as

select tb1.zone,
sum(tb1.pressure = 'Yes') as Yes,
sum(tb1.pressure = 'No') as No
from tb1
group by tb1.zone

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.