日期:2014-05-18  浏览次数:20885 次

求助高手!这个where isnull(spec,'q')='q'子句为什么能够做到的呢????
我有这样一张表:
表goods
id name spec
1 aa NULL
2 bb q
3 cc ww
4 dd q
5 ee NULL
6 ff NULL
7 aa q

我要查询出 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 goods
where name ='aa' and spec='q' or name ='aa' and spec is null


select *,isnull(spec,'q')= from goods
where 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'