版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qichangjian/article/details/88025296
sql查询join时候出现null如何处理?
思路:
将id为null的手动赋值为0或unknown,求出最终结果之后,再将id为0或unknown的改为null,即可
mysql> select case when id is NULL then 0 else id end as id,cost,year from 表;
具体例子
表一:
aid | aname |
---|---|
1 | a |
2 | b |
3 | c |
4 | d |
表二:
aid | aname |
---|---|
2 | b |
3 | c |
3 | NUll |
4 | d |
5 | f |
关联查询出现null:
select ta.aid,ta.aname,tb.bid,tb.bname from ta,tb where ta.aid = tb.bid;
把NUll替换:case when is Null then else end
select
ta.aid,ta.aname,
case when tb.bid is Null then "unkow" else tb.bid end as bid,
case when tb.bname is Null then "unkow" else tb.bname end as bname
from ta
left join tb
on ta.aid = tb.bid;