SQL Select 如果为空则返回默认值
数据库: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)))
问题是,我有几个没有图片的“列表”,并且由于这个 SQL 脚本,它们没有显示。我怎样才能让他们出现?
如果该值为空,是否可以为 Pictures.Picture 列指定“default.jpg”值?我对此很迷茫,所以如果有人能提供帮助,那就太好了。抱歉,如果我问的问题也很糟糕,我不明白如何真正问我需要它做什么。但请询问更多详细信息,我会将其发布。
每个列表可以有用户想要的任意数量的图片,我需要这个脚本来显示列表,即使它没有图片。
第二阶段
谢谢大家。到目前为止,我正在学习一些我什至不知道存在的新命令。现在的问题是它为列表中的每张图片返回一行。但默认图像效果很好。
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
我怎样才能得到它,以便每个 ListingID 只返回 1 行?
Database: 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?
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.
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
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
地雷,你用的是什么数据库?
如果是sql server 2005以上或者oracle,可以使用pivot命令来实现。
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.
需要进行 LEFT join
Need to do a LEFT join
使用
左外连接
而不是内连接
内连接
当且仅当有满足连接的结果时才会返回结果。左外连接
将从左侧侧表返回结果,如果满足连接,还会添加右侧侧表的结果。如果您需要转换从不满足连接返回的空值,然后使用
coalesce
函数,例如coalesce(Pictures.Pictures, 'default.jpg')< /代码>
Use
left outer join
instead ofinner join
Inner join
will return results if and only if there is a result that satisfies the join.Left outer join
will return results from the left side table, and if the join is satisfied also add results from the right side table..If you need to convert the null values returned from the non-satisfying joins, then use
coalesce
function likecoalesce(Pictures.Picture, 'default.jpg')
如果你想在Pic为空时设置默认值,你可以通过COALESCE关键字来做到这一点:
你也可以通过IsNull来实现这一点,如下所示:
你可以阅读这里关于 IsNull 和 Coalesce
if you want to set the default value if the Pic is null you can do this via COALESCE key word:
You can also achieve this via IsNull like below:
you can read here about IsNull and Coalesce
有两件事:
左外连接
而不是内连接
来获取所有列表,即使缺少图片。使用
coalesce
应用默认值EDIT 限制为一行:
Two things:
left outer join
instead ofinner join
to get all the listings, even with missing pictures.Use
coalesce
to apply the defaultEDIT To limit to one row: