sql server - SQL Select Return Default Value If Null -
database: ms sql 2008
select listing.title, listing.mls, pictures.pictureth, pictures.picture, listing.id listing inner join pictures on listing.id = pictures.listingid (pictures.id = (select min(id) pictures (listingid = listing.id)))
the issue is, have several "listings" without picture, , because of sql script don't show up. how can them show up?
maybe give pictures.picture column value of "default.jpg" if value null? i'm pretty lost on this, if help, that'd amazing. sorry if i'm asking question poorly well, dont understand how ask need do. ask more details , i'll post them.
each listing can have many pictures user wants, need script display listing if doesn't have picture.
phase 2
thank all. far i'm learning new commands never knew existed. issue returning row each picture listing has. default image working great.
select listing.title, listing.mls, coalesce(pictures.pictureth, '../default_th.jpg') pictureth, coalesce(pictures.picture, '../default.jpg') picture, listing.id listing left outer join pictures on listing.id = pictures.listingid
how can returns 1 row per listingid ?
two things:
- use
left outer join
instead ofinner join
listings, missing pictures. use
coalesce
apply defaultselect listing.title , listing.mls , pictures.pictureth , coalesce(pictures.picture, 'default.jpg') picture , listing.id listing left outer join pictures on listing.id = pictures.listingid
edit limit 1 row:
select listing.title , listing.mls , pictures.pictureth , coalesce(pictures.picture, 'default.jpg') picture , listing.id listing left outer join pictures on listing.id = pictures.listingid pictures.id null or pictures.id = (select min(id) pictures (listingid = listing.id)))
Comments
Post a Comment