gpt4 book ai didi

php - 如何在 WHERE 子句中使用 CASE WHEN?

转载 作者:行者123 更新时间:2023-11-29 10:41:05 27 4
gpt4 key购买 nike

这是我的查询的一部分:

WHERE CASE $range WHEN 'ALL' THEN TRUE
ELSE $this->table_alias.date_time > unix_timestamp(DATE_SUB(now(), INTERVAL 1 $range))
END

注意到$range是一个包含单词的php变量。它抛出此错误消息:

Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'ALL WHEN 'ALL' THEN TRUE ELSE re.date_time > unix_timestam' at line 13 in C:\xampp\htdocs\myweb\others\users.php:120 Stack trace: #0 C:\xampp\htdocs\myweb\others\users.php(120): PDO->prepare('SELECT u.id use...') #1 C:\xampp\htdocs\myweb\others\questions.php(359): users->index(' AND categories...', ' INNER JOIN qan...', 'tagged') #2 C:\xampp\htdocs\myweb\others\users.php(26): questions->tagged('index') #3 C:\xampp\htdocs\myweb\application\other.php(24): users->index() #4 C:\xampp\htdocs\myweb\index.php(161): require_once('C:\xampp\htdocs...') #5 {main} thrown in C:\xampp\htdocs\myweb\others\users.php on line 120

有人出什么问题了吗?

最佳答案

我认为你使用了错误的语法

$sql = "WHERE CASE 
WHEN '{$range}' = 'ALL' THEN 1=1
ELSE {$this->table_alias}.date_time > unix_timestamp(DATE_SUB(now(), INTERVAL 1 $range))
END";

关于php - 如何在 WHERE 子句中使用 CASE WHEN?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45469600/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com