我有一个在mysql中工作正常但我需要将其迁移到db2的查询。
这是mysql查询:
SELECT
message, host, location
FROM
table
WHERE
host = 'VALUE_HOST' OR location = 'VALUE_LOCATION')
OR
(message IS NOT NULL AND host IS NULL AND location IS NULL)
ORDER BY
(IF(message IS NOT NULL,1,0)*8 + IF(host = 'VALUE_HOST',1,0)*4 + IF(location = 'VALUE_LOCATION',1,0)*2) DESC
关键是如何根据这些条件替换“order by”部分中的函数,我得到相同的排序结果?
答案 0 :(得分:1)
DB2没有IF函数,但它有CASE表达式:
CASE WHEN message IS NOT NULL THEN 1 ELSE 0 END
或者,如果您只是测试相等性:
CASE host WHEN 'VALUE_HOST' THEN 1 ELSE 0 END