I have defined:
table X
ID AGE HEIGHT REGISTERED
1 23 1.83 1
2 24 1.81 0
3 24 1.72 0
4 22 1.91 1
5 32 1.76 0
And I would like to perform a query to return valid values but *ONLY* for the registered ID’s. Those who are not, would get 0 in return. like this:
result
ID AGE HEIGHT REGISTERED
1 23 1.83 1
2 0 0 0
3 0 0 0
4 22 1.91 1
5 0 0 0
What is the way of defining that query?
You can use the MySQL specific
IFexpression:See it working online: sqlfiddle
You could also use the standard SQL
CASEexpression:In response to your comment here’s one more that might be slightly easier if you have lots of columns:
See it working online: sqlfiddle
Note that the column list in this query could be shortened to just
SELECT *and the query would still work. But usingSELECT *in production code is not advisable for various reasons that have been covered elsewhere on the site.