case - How to SELECT certain rows based on comparison of 2 columns in a MySQL Table -
i have table follows:
id male female m_age f_age 1 bob jane 27 25 2 sam sue 29 31 3 fred barb 22 22 4 dave joan 30 28
i want return name of person older. result:
bob sue dave
my might mysql query this?
here have far.
$result = mysql_query(' select case when m_age > f_age male when m_age < f_age female end person people ') or die(mysql_error());
but query returning blank row on fred equaling barb , not want row returned.
thanks
you can include where
clause exclude rows when ages same:
select case when m_age > f_age male when m_age < f_age female end person people m_age <> f_age
see sql fiddle demo
or can include else
value return if age's match:
select case when m_age > f_age male when m_age < f_age female else 'ages match' end person people;
see sql fiddle demo
Comments
Post a Comment