gpt4 book ai didi

MySQL总结一些CASE WHEN函数

转载 作者:行者123 更新时间:2023-11-30 00:07:51 25 4
gpt4 key购买 nike

当我期望任何 CASE 都有三个不同的表达式时,是否有可能将三个 CASE 函数最小化为一个?

因此,所有 CASE 调用都依赖于一个值 (con.category_id)。我希望您能帮助我通过保留整个功能来清理此 SQL 语句。

SELECT 
UNIX_TIMESTAMP(con.content_date_publishing) AS time,
CASE
when con.category_id IS NOT NULL
THEN
con.category_id
ELSE
0
END AS category_id,
CASE
when con.category_id IS NOT NULL
THEN
cat.category_name_de
ELSE
NULL
END AS category_name
FROM
v5_content AS con,
v5_category AS cat
WHERE content_id = 2 AND
CASE
when con.category_id IS NOT NULL
THEN
con.category_id = cat.category_id
ELSE
1

END
LIMIT
1

最佳答案

您似乎缺少 LEFT JOIN 运算符。

SELECT
UNIX_TIMESTAMP(con.content_date_publishing) AS time,
cat.category_id,
cat.category_name_de
FROM v5_content as con
LEFT JOIN v5 category as cat ON con.category_id = cat.category_id
WHERE
content_id = 2;

关于MySQL总结一些CASE WHEN函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24364181/

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