case 可能是 sql 中被误用最多的关键字之一。虽然你可能以前用过这个关键字来创建字段,但是它还具有更多用法。例如,你可以在 where 子句中使用 case。 首先让我们看一下 case 的语法。在一般的 select 中,其语法如下: select <mycolumnspec> = case when <a> then <somethinga> when <b> then <somethingb> else <somethinge> end 在上面的代码中需要用具体的参数代替尖括号中的内容。下面是一个简单的例子: use pubs go select title, 'price range' = case when price is null then 'unpriced' when price < 10 then 'bargain' when price between 10 and 20 then 'average' else 'gift to impress relatives' end from titles order by price go
|