日期:2014-05-17  浏览次数:20751 次

如何将查出的没有返回结果的显示成null ,0这样的
SQL code

SELECT NAME,AGE,SALES FROM USER WHERE DATE_TIME=TO_DATE('2011-11-21','yyyy-MM-dd') AND NAME='hello';


这个SQL语句查询在2011-11-21这天的hello用户的数据 但是在这天没有该用户的信息
查询后没有NULL或者0显示 
NAME AGE SALES
如何能让他显示成
NAME AGE SALES
hello 0 0  

请问这样该如何做

------解决方案--------------------
用用户表来 left join
------解决方案--------------------
SQL code

seelct a.name ,nvl(b.age,0) as age,nvl(b.sales,0) as sales
from 
(select 'hello' as name from dual) a left join USER b on a.name=b.name
where b.DATE_TIME=TO_DATE('2011-11-21','yyyy-MM-dd')

------解决方案--------------------
SQL code
seelct a.name ,nvl(b.age,0)  age,nvl(b.sales,0)  sales
from 
(select name from table_user where name='hello') a left join USER b on a.name=b.name
where b.DATE_TIME=TO_DATE('2011-11-21','yyyy-MM-dd')

------解决方案--------------------
参考代码:
SQL code

SELECT NAME, NVL(AGE, 0), NVL(SALES, 0)
FROM USER 
WHERE DATE_TIME=TO_DATE('2011-11-21','yyyy-MM-dd') AND NAME='hello';