I have a table y
Which has two columns a
and b
Entries are
a b
1 2
1 3
1 4
0 5
0 2
0 4
I want to get 2,3,4 if I search column a
for 1, and 5,2,4 if I search column a
.
So, if I search A for something that is in A, (1) I get those rows, and if there are no entries A for given value, give me the 'Defaults' (a
= '0')
Here is how I would know how to do it.
$r = mysql_query('SELECT `b` FROM `y` WHERE `a` = \'1\';');
//This gives desired results, 3 rows
$r = mysql_query('SELECT `b` FROM `y` WHERE `a` = \'2\';');
//This does not give desired results yet.
//Get the number of rows, and then get the 'defaults'
if(mysql_num_rows($r) === 0) $r = mysql_query('SELECT `b` FROM `y` WHERE `a` = 0;');
So, now that it's sufficiently explained, how do I do that in one query, and what about performance concerns? The most used portion would be the third query, because there would only be values in a
for a number IF you stray from the defaults.