【发布时间】:2010-09-11 14:50:41
【问题描述】:
我有一张表 y 有两列 a 和 b
条目是:
a b
1 2
1 3
1 4
0 5
0 2
0 4
如果我在 a 列中搜索 1,我想获得 2、3、4,如果我在 a 列中搜索,我想获得 5、2、4。
因此,如果我在 A 中搜索 A 中的某些内容,(1) 我会得到这些行,如果给定值没有条目 A,则给我“默认值”(a = '0')
我知道该怎么做:
$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;');
那么,既然已经充分解释了,我该如何在一个查询中做到这一点,以及性能问题呢?
最常用的部分是第三个查询,因为如果您偏离默认值,a 中只会有一个数字的值。
【问题讨论】: