我有这样一张表:
表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 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'