我正在像这样动态查询:
$query = "SELECT u.* FROM users u WHERE date_time > unix_timestamp(DATE_SUB(now(), INTERVAL 1 $range))";
还$range包含这样的单词:
$range
switch ($_GET['range']){ case "week": $range = "WEEK"; case "month": $range = "MONTH"; case "year": $range = "YEAR"; case "ALL": $range = <I don't know what should I put here to get the expected result>; }
就像我在上面说过的,我想设置一些$range值作为值使WHERE子句类似WHERE 1。我怎样才能做到这一点?
WHERE
WHERE 1
我认为亚当的建议是这样的:
$where = ''; switch ($_GET['range']) { case "week": $where = "date_time > unix_timestamp(DATE_SUB(now(), INTERVAL 1 WEEK))": break; case "month": $where = "date_time > unix_timestamp(DATE_SUB(now(), INTERVAL 1 MONTH))": break; case "year": $where = "date_time > unix_timestamp(DATE_SUB(now(), INTERVAL 1 YEAR))": break; default: $where = ''; } $query = "SELECT u.* FROM users u WHERE $where";
假设使用其他条件进行报告?字符串连接变得混乱的地方。在零件之间添加“与”。完全禁止使用WHERE关键字吗?其中表示SQL查询的OOP代码可能很有用。