0

我的查询:

SELECT s.artist_id AS ArtistID,
       s.name AS Song,
       at.name AS artistname,
       a.name AS Albumname
  FROM album s
       LEFT OUTER JOIN song a ON s.id = a.album_id
       LEFT OUTER JOIN artist at ON at.id = s.artist_id;

歌曲和专辑表中的 null

对于加入表格后的上述查询,报告中有空值(演示结果)

实际结果

A B     
1 2                                                        
3 

预期结果

A B                                   
1 2                                                  
3 *  

我想在 excel 中打印上述结果而不是 null 它应该替换为任何字符。你能帮我怎么做吗?

4

2 回答 2

2

对此的 ISO/ANSI 标准函数是COALESCE()

SELECT a.artist_id AS ArtistID,
       a.name AS Song,
       COALESCE(ar.name, '*') AS artistname,
       COALESCE(s.name, '*') AS Albumname
FROM album a LEFT OUTER JOIN
     song s
     ON a.id = d.album_id LEFT OUTER JOIN
     artist ar
     ON ar.id = a.artist_id;

请注意,我更改了表别名,因此它们是表名的缩写。

于 2018-12-11T11:38:04.583 回答
0

一种选择是使用该NVL功能,例如

nvl(a.name, '*') as albumname

或者,DECODE可能会有所帮助:

decode(a.name, null, '*', a.name) as albumname

或者CASE

case when a.name is null then '*'
     else a.name
end as albumname
于 2018-12-11T11:05:01.847 回答