43

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 ?

2
  • did you try the with the WHERE part of your original query ? Commented Oct 7, 2010 at 23:00
  • I have, and it doesn't work. I'll post the server info if people want to try some statements of their own? I'll create a new dummy database. Commented Oct 7, 2010 at 23:10

5 Answers 5

56

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))) 
Sign up to request clarification or add additional context in comments.

3 Comments

But how do I make it so it only returns 1 row per listing and not as many rows as there are pictures?
If there is more than one Picture but you only want one row returned, what are the rules for which Picture to use?
Sorry, didn't see the where clause on your original query. Added that, with a test to include rows where there is no matching Picture.
12

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

Comments

4

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

Comments

2

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

1 Comment

Works, but returns a row for each picture as well. So It gives me 7 results because there are 6 pictures on listing 1 and listing 2 has no pictures, but is returned.
1

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.

1 Comment

MSSQL 2008, I'll post that info at the top too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.