sql where 后面跟 case 的问题

来源:百度知道 编辑:UC知道 时间:2024/07/02 20:51:26
例如:select * from table where case when @a='1' then fields1='number' end and fields2='name'
就是想判断当@a='1'时 where后面是fields1='number' and fields2='name'
当@a=''时 where 后面只有一个判断条件 fields2='name'

SQL 帮助文档的例子
SELECT Category =
CASE type
WHEN 'popular_comp' THEN 'Popular Computing'
WHEN 'mod_cook' THEN 'Modern Cooking'
WHEN 'business' THEN 'Business'
WHEN 'psychology' THEN 'Psychology'
WHEN 'trad_cook' THEN 'Traditional Cooking'
ELSE 'Not yet categorized'
END,
CAST(title AS varchar(25)) AS 'Shortened Title',
price AS Price
FROM titles
WHERE price IS NOT NULL
ORDER BY type, price
COMPUTE AVG(price) BY type
GO