Say I have the following query:
Code: Select all
$result = mysql_query("SELECT users1.name, users1.age, COUNT(users2.age) AS Rank
FROM users users1
JOIN users users2 ON users1.age < users2.age OR (users1.age=users2.age and users1.name = users2.name)
WHERE users1.name='Bob'
GROUP BY users1.name, users1.age
ORDER BY users1.age DESC, users1.name DESC
;
") or die ("error 3: " . mysql_error());
$result2 = mysql_fetch_array($result);
print "Bob is the " . $result2['Rank'] . " oldest person<BR>";How can I add into that query a field "gender", so that it will only factor in users that are `gender`='Male' so that I can find out that Bob is the __ oldest man instead of the ___ oldest person?
I tried many combinations of putting a WHERE clause around in that query but most of my attempts ended up with an error; I couldn't figure out where exactly in the query it should go.