我想制作可选的sql查询参数,问题是当$param['a']
和$param['b']
都为真时。然后$ query看起来像这个SELECT * from table WHERE something = TRUE AND column IS NOT NULL AND column IS NULL)
。我怎么解决这个问题?
switch($param)
{
case $param['a']:
$query = "AND column IS NOT NULL";
case $param['b']:
$query = "AND column IS NULL";
}
答案 0 :(得分:0)
使用if条件更换开关。
通过使用if条件和连接,您可以实现所需的查询。例如,如果您有两个参数
让您初步查询
$query = 'SELECT * from table WHERE something = TRUE';
// based on the params concate conditions to initial query
if($param['a']){
$query .= ' AND column IS NOT NULL';
}
if($param['b']){
$query .= ' AND column IS NULL';
}
如果两个参数都为真,则结果查询将为
SELECT * from table WHERE something = TRUE AND column IS NOT NULL AND column IS NULL
您可以根据参数添加所需的条件。