SQL 选择返回默认值如果为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3886340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SQL Select Return Default Value If Null
提问by Landmine
Database: MS SQL 2008
数据库:MS SQL 2008
SELECT Listing.Title, Listing.MLS, Pictures.PictureTH, Pictures.Picture, Listing.ID
FROM Listing INNER JOIN Pictures ON Listing.ID = Pictures.ListingID
WHERE (Pictures.ID = (SELECT MIN(ID) FROM Pictures WHERE (ListingID = Listing.ID)))
The issue is, I have several "Listings" without a Picture, and because of this SQL script they don't show up. How can I get them to show up?
问题是,我有几个没有图片的“列表”,并且由于这个 SQL 脚本,它们没有出现。我怎样才能让他们出现?
Maybe give the Pictures.Picture Column a value of "default.jpg" if the value is null? I'm pretty lost on this, so if someone could help, that'd be amazing. Sorry if I'm asking the question poorly as well, I dont understand how to ask really what I need it to do. But ask for more details and I'll post them.
如果值为空,也许给 Pictures.Picture Column 一个值“default.jpg”?我对此很迷茫,所以如果有人可以提供帮助,那就太棒了。对不起,如果我问的问题也很糟糕,我不明白如何真正问我需要它做什么。但是询问更多详细信息,我会发布它们。
Each Listing can have as many pictures as the user wants, I need this script to display a Listing even if it doesn't have a picture.
每个列表可以有用户想要的任意数量的图片,我需要这个脚本来显示列表,即使它没有图片。
PHASE 2
阶段2
Thank you all. So far I'm learning some new commands I never even knew existed. The issue now is its returning a row for each picture a listing has. But the default image is working great.
谢谢你们。到目前为止,我正在学习一些我什至不知道存在的新命令。现在的问题是它为列表中的每张图片返回一行。但是默认图像效果很好。
SELECT Listing.Title, Listing.MLS, coalesce(Pictures.PictureTH, '../default_th.jpg') as PictureTH, coalesce(Pictures.Picture, '../default.jpg') as Picture, Listing.ID FROM Listing LEFT
OUTER JOIN Pictures ON Listing.ID = Pictures.ListingID
How can I get it so it only returns 1 row per ListingID ?
我怎样才能得到它,以便每个 ListingID 只返回 1 行?
回答by Shannon Severance
Two things:
两件事情:
- Use
left outer join
instead ofinner join
to get all the listings, even with missing pictures. Use
coalesce
to apply the defaultSELECT Listing.Title , Listing.MLS , Pictures.PictureTH , coalesce(Pictures.Picture, 'default.jpg') as Picture , Listing.ID FROM Listing LEFT OUTER JOIN Pictures ON Listing.ID = Pictures.ListingID
- 使用
left outer join
而不是inner join
获取所有列表,即使缺少图片。 使用
coalesce
应用默认SELECT Listing.Title , Listing.MLS , Pictures.PictureTH , coalesce(Pictures.Picture, 'default.jpg') as Picture , Listing.ID FROM Listing LEFT OUTER JOIN Pictures ON Listing.ID = Pictures.ListingID
EDITTo limit to one row:
编辑限制为一行:
SELECT Listing.Title
, Listing.MLS
, Pictures.PictureTH
, coalesce(Pictures.Picture, 'default.jpg') as Picture
, Listing.ID
FROM Listing
LEFT OUTER JOIN Pictures
ON Listing.ID = Pictures.ListingID
WHERE Pictures.ID is null
OR Pictures.ID = (SELECT MIN(ID)
FROM Pictures
WHERE (ListingID = Listing.ID)))
回答by Vijjendra
if you want to set the default value if the Pic is null you can do this via COALESCE key word:
如果您想在 Pic 为空时设置默认值,您可以通过 COALESCE 关键字执行此操作:
SELECT Listing.Title, Listing.MLS, Pictures.PictureTH,
COALESCE (Pictures.Picture, 'default.jpg') AS Pictures, Listing.ID
FROM Listing LEFT JOIN Pictures
ON Listing.ID = Pictures.ListingID
WHERE (Pictures.ID = (SELECT MIN(ID)
FROM Pictures WHERE (ListingID = Listing.ID)))
You can also achieve this via IsNull like below:
您还可以通过 IsNull 实现这一点,如下所示:
SELECT Listing.Title, Listing.MLS, Pictures.PictureTH,
ISNULL(Pictures.Picture, 'default.jpg') AS Pictures, Listing.ID
FROM Listing LEFT JOIN Pictures
ON Listing.ID = Pictures.ListingID
WHERE (Pictures.ID = (SELECT MIN(ID)
FROM Pictures WHERE (ListingID = Listing.ID)))
you can read hereabout IsNull and Coalesce
您可以在此处阅读有关 IsNull 和 Coalesce 的信息
回答by Gabriele Petrioli
Use left outer join
instead of inner join
使用left outer join
代替inner join
Inner join
will return results if and only if there is a result that satisfies the join.
Inner join
当且仅当存在满足连接的结果时才会返回结果。
Left outer join
will return results from the leftside table, and if the join is satisfied also add results from the rightside table..
Left outer join
将返回左侧表的结果,如果满足连接,还添加右侧表的结果。
If you need to convert the null values returned from the non-satisfyingjoins, then use coalesce
function like coalesce(Pictures.Picture, 'default.jpg')
如果您需要转换从非满足连接返回的空值,则使用coalesce
类似的函数coalesce(Pictures.Picture, 'default.jpg')
回答by ozczecho
Need to do a LEFT join
需要做一个LEFT join
SELECT Listing.Title, Listing.MLS, Pictures.PictureTH, Pictures.Picture, Listing.ID
FROM Listing LEFT JOIN Pictures ON Listing.ID = Pictures.ListingID
回答by clyc
Landmine, what database are you using?
地雷,你用的是什么数据库?
If it's sql server 2005 or above or oracle, you can use the pivot command to achieve this.
如果是sql server 2005以上或者oracle,可以使用pivot命令来实现。