首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

高手!这个where isnull(spec,'q')='q'子句为什么能够做到的呢?

2012-03-12 
求助高手!这个where isnull(spec,q)q子句为什么能够做到的呢????我有这样一张表:表goodsidnamespec1a

求助高手!这个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'

热点排行