Solved

SQL Server 2005 Join Table on pk and displayorder field

Posted on 2008-06-15
5
256 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

864 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

25 Experts available now in Live!

Get 1:1 Help Now