好的,我有2列:购买者 - 订单
如何编写案例陈述以显示:
case when purchaser is blank, then orders is blank
我试过
case when purchaser = ' ' then orders = ' ' else orders end as OrdersEnd
答案 0 :(得分:1)
正确的语法如下:
SELECT
...
, CASE WHEN purchaser = '' THEN '' ELSE orders END AS OrdersEnd
FROM ...
请注意"购买者是空白的"可能意味着"是空的或空的"。在这种情况下,语法应为
CASE WHEN purchaser is NULL OR purchaser = '' THEN '' ELSE orders END AS OrdersEnd
答案 1 :(得分:0)
试试这个:
case when purchaser = '' then '' else orders end as OrdersEnd
答案 2 :(得分:0)
"空白"可能意味着NULL
或空格。你没有提到数据库,但以下内容最适用:
(case when purchase is null or replace(purchaser, ' ', '') = '' then ' '
else orders
end) as OrdersEnd
答案 3 :(得分:0)
您需要检查null和只有空格的购买者。
在购买者无效的情况下,可能还有其他情况......您可以在更多WHEN部分添加这些内容:
SELECT
CASE WHEN purchaser is null THEN ''
WHEN TRIM(purchaser) = '' THEN ''
ELSE orders END as Orders
FROM table
现在我知道这是MS SQL,这是更短的
SELECT CASE WHEN TRIM(ISNULL(purchaser,'')) = '' THEN '' ELSE orders END AS Orders
FROM table
答案 4 :(得分:0)
如果'blank'表示NULL,则可以使用特定于DB的函数。
甲骨文:
SELECT NVL2(purchaser, orders, '') FROM MyTable;
MySQL的:
SELECT IF(purchaser, orders, '') FROM MyTable;