Solved

SQL Server 2005 Join Table on pk and displayorder field

Posted on 2008-06-15
5
245 Views
Last Modified: 2010-04-21
Hi,
I have two tables Listing and ListingPictures

What I would like to do is query these tables for listing data plus the listing picture with the lowest display order. Fields and sample data below:

Listing Fields:
ListingID | StreetAddress | City | State | Zip | etc

ListingPictures Fields
ListingPictureID | ListingID | FileName | DisplayOrder

Sample Listing Data
17 | 12345 Some Street West | Miami | Florida | 33161
18 | 23456 Some Street East | West Palm Beach | Florida | 33151

Sample ListingPictures Data:
29 | 17 | ListingPictures/12345.jpg | 1
30 | 17 | ListingPictures/23456.jpg | 2
31 | 18 | ListingPictures/34567.jpg | 1
32 | 18 | ListingPictures/45678.jpg | 2

So the ideal query for me would return results like below
ListingID | ListingPictureID | ListingPictureFileName      | StreetAddress | City        | State        | Zip
17          | 29                      | ListingPictures/12345.jpg | The Address    | The City | The State | The Zip
18          | 31                      | ListingPictures/34567.jpg | The Address    | The City | The State | The Zip

If anyone could help with that query I would greatly appreciate it.
Thanks in advance!
0
Comment
Question by:amagondes
  • 3
  • 2
5 Comments
 
LVL 3

Expert Comment

by:darkmooink
ID: 21789854
select * from Listing join ListingPictures on ListingPictures.ListingID = Listing.ListingID where DisplayOrder= 1
0
 

Author Comment

by:amagondes
ID: 21790446
Hi darkmooink,
thanks for the reply. the problem is that there is not always guaranteed to be a display order of 1. would it be possible to do something like:

SELECT Listing.*, ListingPicture.ListingPictureID, ListingPicture.FileName FROM Listing
LEFT JOIN ListingPicture ON ListingPicture.ListingID = Listing.ListingID
WHERE ListingPicture.DisplayOrder = (SELECT MIN(ListingPicture.DisplayOrder) FROM ListingPictures WHERE ListingPicture.ListingID = Listing.ListingID  )

This seems to work, but is there a more efficient way to write this query?

0
 

Author Comment

by:amagondes
ID: 21790465
Actually, if there are no pictures for a listing the listing won't get returned at all. i would like it to return listings whether they have a picture(s) or not, but if they do have a picture then return the picture with the lowest display order. thanks for the help
0
 
LVL 3

Accepted Solution

by:
darkmooink earned 500 total points
ID: 21792848
as for the not displaying of the record if there isnt a record on listingPicture you could add " or ListingPicture.DisplayOrder is null" to the end of the query that seems to work for some data i have.
but streamlining it i dont think i can help. i have tryed a few things but i eather dont get the results i want or i error.
0
 

Author Closing Comment

by:amagondes
ID: 31467407
Thanks for your help
0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now