Solved

SQL Server 2005 Join Table on pk and displayorder field

Posted on 2008-06-15
5
294 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

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 …
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

729 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