包括所有在哪些情况下进行一次SQL查询

时间:2013-05-20 05:02:30

标签: php sql

这是代码

if ($st) active_code = '1';

if (!$st) active_code > '0';


SELECT username FROM users WHERE active_code = '1'

SELECT username FROM users WHERE active_code > '0'

是否有办法进行一次SQL查询?

请问好吗?

2 个答案:

答案 0 :(得分:3)

$cond = $st ? "= '1'":" > '0'";
$sql = "SELECT username FROM users WHERE active_code $cond";

答案 1 :(得分:2)

只需定义一个包含条件的变量:

if ($st) {
    $condition = "active_code = '1'";
} else {
    $condition = "active_code > '0'";
}

$sql = "SELECT username FROM users WHERE $cond";