In my Events table I have a column called Pos1, which contains IDs from A to E. I need to count how many times ‘E’ appears in the column and where it ranks alongside the other numbers.
For example in this table ‘E’ occurs 1 time and is ranked 4 (D=3, A=2, C=2, E=1, B=0).
Pos1
E
C
D
C
D
D
A
A
I’m a complete SQL amateur so the closest I’ve got is an array printing the count of each ID, but I need this to be limited to counting a single ID and printing the rank.
$query = "SELECT Pos1, COUNT(Pos1) FROM Events GROUP BY Pos1";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo $row['COUNT(Pos1)'] ."<br />";
}
try this:
SQLFIDDLE DEMO HERE
EDIT: php code