SQL Select 如果为空则返回默认值

发布于 2024-09-26 10:23:31 字数 946 浏览 5 评论 0原文

数据库: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

叫思念不要吵 2024-10-03 10:23:43

地雷,你用的是什么数据库?

如果是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.

风吹短裙飘 2024-10-03 10:23:42

需要进行 LEFT join

SELECT Listing.Title, Listing.MLS, Pictures.PictureTH, Pictures.Picture, Listing.ID 
FROM Listing LEFT JOIN Pictures ON Listing.ID = Pictures.ListingID

Need to do a LEFT join

SELECT Listing.Title, Listing.MLS, Pictures.PictureTH, Pictures.Picture, Listing.ID 
FROM Listing LEFT JOIN Pictures ON Listing.ID = Pictures.ListingID
小忆控 2024-10-03 10:23:41

使用左外连接而不是内连接

内连接当且仅当有满足连接的结果时才会返回结果。

左外连接将从左侧侧表返回结果,如果满足连接,还会添加右侧侧表的结果。

如果您需要转换从不满足连接返回的空值,然后使用coalesce函数,例如coalesce(Pictures.Pictures, 'default.jpg')< /代码>

Use left outer join instead of inner 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 like coalesce(Pictures.Picture, 'default.jpg')

兔小萌 2024-10-03 10:23:40

如果你想在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)))

你也可以通过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)))

你可以阅读这里关于 IsNull 和 Coalesce

if you want to set the default value if the Pic is null you can do this via COALESCE key word:

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:

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 here about IsNull and Coalesce

完美的未来在梦里 2024-10-03 10:23:39

有两件事:

  1. 使用左外连接而不是内连接来获取所有列表,即使缺少图片。
  2. 使用coalesce应用默认值

    选择列表.标题
        , 列表.MLS
        , 图片.PictureTH
        ,合并(Pictures.Picture,'default.jpg')作为图片
        , 列表.ID  
    来自列表 
    左外连接图片 
        ON Listing.ID = 图片.ListingID 
    

EDIT 限制为一行:

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))) 

Two things:

  1. Use left outer join instead of inner join to get all the listings, even with missing pictures.
  2. Use coalesce to apply the default

    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 
    

EDIT To 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))) 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文