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 ?
By default, the columns are able to hold NULL values. A NOT NULL constraint in SQL is used to prevent inserting NULL values into the specified column, considering it as a not accepted value for that column.
Here's one way: SELECT *, IFNULL( ( SELECT col1 FROM table1 WHERE col1 IN ('012311','0123631','091233','092111') ), 'some_value' ) AS my_col1 FROM table1; Not neccessarily copy+paste, you will have to adjust for your specific case.
In Object Explorer, right-click the table with columns for which you want to change the scale and select Design. Select the column for which you want to specify a default value. In the Column Properties tab, enter the new default value in the Default Value or Binding property.
The IS NULL condition is used in SQL to test for a NULL value. It returns TRUE if a NULL value is found, otherwise it returns FALSE. It can be used in a SELECT, INSERT, UPDATE, or DELETE statement.
Two things:
left outer join
instead of inner join
to get all the listings, even with missing pictures.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)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With