读书人

高手!这个where isnull(spec,#039;q#039;)=#039;q#039;

发布时间: 2012-03-12 12:45:33 作者: rapoo

求助高手!这个where isnull(spec,'q')='q'子句为什么能够做到的呢????
我有这样一张表:
表goods
idnamespec
1aaNULL
2bbq
3ccww
4ddq
5eeNULL
6ffNULL
7aaq

我要查询出 name = aa ,spec = q或null 的数据.
一般情况下是
where name ='aa' and spec='q' or name ='aa' and spec is null

我见别人这样写也能够实现:
select * from goods
where name = 'aa' and isnull(spec,'q')='q'

这到底是为什么呢?
能详细讲解一下吗?

[解决办法]

SQL code
--看看最后一列select *,isnull(spec,'q') from goodswhere name ='aa' and spec='q' or name ='aa' and spec is nullselect *,isnull(spec,'q')= from goodswhere name = 'aa' and isnull(spec,'q')='q'
[解决办法]
效果一样的,
isnull(spec,'q') 的意思就是 如果spec is null 就默认为 spec ='q'
[解决办法]
isnull(spec,'q') 等价于 case when spec is null then 'q'

读书人网 >SQL Server

热点推荐