gpt4 book ai didi

php - 尝试在 CASE WHEN mysql php 中使用 CASE WHEN ?

转载 作者:行者123 更新时间:2023-11-29 20:33:46 25 4
gpt4 key购买 nike

看看我的代码,我想实现类似的效果,当 cat='1' 时,然后 CASE 或当 cat='2' 时,然后 CASE。帮忙,感激不尽。

<?php
$test ="SELECT DISTINCT dis from $table WHERE
(
CASE When cat='1' THEN (CASE
WHEN val2 ='g' THEN val1 > %d
WHEN val2 ='l' THEN val1 < %d
ELSE val1 !=%d END)

When cat='2' THEN (CASE
WHEN val2 ='g' THEN val1 > %d
WHEN val2 ='l' THEN val1 < %d
ELSE val1 !=%d END)
ELSE END
)
AND dis IN ('19','20','21')
";
?>

最佳答案

在一个 CASE 语句中尝试一下,看看它是否返回预期的结果:

<?php
$test ="SELECT DISTINCT dis from $table
WHERE CASE
WHEN cat='1' AND val2 ='g' THEN val1 > %d
WHEN cat='1' AND val2 ='l' THEN val1 < %d
WHEN cat='1' THEN val1 !=%d
WHEN cat='2' AND val2 ='g' THEN val1 > %d
WHEN cat='2' AND val2 ='l' THEN val1 < %d
WHEN cat='2' THEN val1 !=%d
END
AND dis IN ('19','20','21')
";
?>

此外,根据 cat = 1 或 2 的不同,看起来没有任何差异,因此您可以将 CASE 减少为:

CASE 
WHEN cat in ('1','2') AND val2 ='g' THEN val1 > %d
WHEN cat in ('1','2') AND val2 ='l' THEN val1 < %d
WHEN cat in ('1','2') THEN val1 !=%d
END

关于php - 尝试在 CASE WHEN mysql php 中使用 CASE WHEN ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38902480/

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