*The actual problem is quite more complex than the post title :S
Suppose I have table
ID | vote_id | vote_type | vote_user
1 ---- 2 ----- up ------- Tom
2 ---- 3 ----- up ------- John
3 ---- 3 ----- down ----- Harry
4 ---- 4 ----- up ------- Tom
5 ---- 2 ----- down ----- John
6 ---- 3 ----- down ----- Tom
So what I want to do is
- Query the table by specific vote_id
- Then Count how many voted up and how many voted down from the query in 1
- Then I want to check also that had John voted for that vote_id or not.
And the way I do is.
$up=0;
$down=0;
$voted="no";
$r=mysql_query("SELECT*FROM table_name WHERE vote_id == 3");
while($row=mysql_fetch_array($r){
if($row["vote_type"]=="up"){ $up++;}else{$down++;}
if($row["vote_user"=="John"){ $voted="yes";}
}
But is there a way ( equivalent code ) to do this without using WHILE LOOP because the actual table can be very large so running while loop can be very exhaustive :S
EDIT Can I do with single query?
In a single query
Or any variation of sum(case when)