Database Management
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesDatabase Management

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 8th, 2004, 09:46 AM
dmors dmors is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 1 dmors User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
select distinct from multiple tables

I have three tables "items", "dates", "membership", and "collection".

"items" joins "dates" on Id, a unique identifier. "collection" defines certain collections of "items", each has a unique collection_id. So "membership" has an Id field, and a collection_id field. Each "items" record can belong to multiple "collection"s.

The query:

Code:
SELECT i.Id, i.Name, d.Date from items as i LEFT JOIN dates as d on (i.Id = d.Id) LEFT JOIN membership as m on (i.Id = m.Id) LEFT JOIN collection as c on (m.collection_id = c.collection_id)


returns the expected results, but I need to only get ONE of each "items" record, regardless of how many memberships it has. The membership record it returns is unimportant. I tried DISTINCT, but I'm not sure I have the syntax down right, and can't seem to find documentation or a sample that applies to this situation.

In another forum, Stumpy suggested:

Code:
SELECT DISTINCT(i.Id), i.Name, d.Date...


but it didn't work as I had hoped, meaning that it didn't limit it as I needed. Is there any other possible solution?

Also, if this isn't implied by my choice of forum: I have to accomplish this all with the SQL, I can't use ASP/PHP/Perl/anything else to accomplish this.

Thanks much for any help.


D. Mors

Reply With Quote
  #2  
Old January 9th, 2004, 01:56 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,690 r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 6 Days 16 h 20 m
Reputation Power: 985
you haven't really explained what you're trying to do

you say your query gets the expected results except more than one row per item -- well, that's becaise each item row is being joined to membership and collection rows

if all you really want is one row per item, do this:
Code:
select i.Id, i.Name, d.Date 
  from items as i 
left outer
   join dates as d 
     on i.Id = d.Id
__________________
r937.com | rudy.ca

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDatabase Management > select distinct from multiple tables


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway