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

Here is the code

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'

Is there is a way to make then one sql query ?

Any idea please ?

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

Just define a variable that will hold the condition:

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

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