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:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old August 5th, 2002, 07:17 PM
jalessio jalessio is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 2 jalessio User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Problem ANDing many to one relationship

DB is MS SQL Server 2000

I have one table that represents files on a shared filesystem and another table that represents labels that are applied to the file. Here's a text representation of the simplified table layout:

|----------|
| files |
|----------|
| file_id |
| filename |
|----------|

|----------|
| labels |
|----------|
| label_id |
| file_id |
| label |
|----------|

Any number of labels can be applied to a file and I need to be able to find out which files have certain labels. The problem I am having is finding files that have more than one label. For instance, "show me all the files that have 'labelone' and 'labeltwo'". I first tried representing the query like this:

SELECT files.* FROM files
INNER JOIN files ON files.file_id = labels.file_id
WHERE labels.label = 'labelone' AND labels.label = 'labeltwo'

I soon realized that this never returns any rows since a single record expressed by the join can never actually have two labels atttached to it. The only way I have been able to get the desired results is via a self join like this:

SELECT files.*
FROM file INNER JOIN
labels L1 ON files.file_id = L1.file_id INNER JOIN
labels L2 ON L1.file_id = L2.file_id
WHERE (L1.assetlabel = 'labelone') AND (L2.assetlabel = 'labeltwo')

This appears to work fine, but I'm wondering if there is a simpler way to express this query since this can quickly get out of hand when dealing many labels. For example, if I want to check for the existance of five different labels I have to self join the labels table as many times. Any ideas/suggestions on how to simplify this type of query? I am open to the idea of redesigning the data structure if necessary. Thanks.

- Jamie

Reply With Quote
  #2  
Old August 6th, 2002, 03:05 PM
dcaillouet's Avatar
dcaillouet dcaillouet is offline
Big Endian
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: May 2001
Location: Fly-over country
Posts: 1,173 dcaillouet User rank is Sergeant (500 - 2000 Reputation Level)dcaillouet User rank is Sergeant (500 - 2000 Reputation Level)dcaillouet User rank is Sergeant (500 - 2000 Reputation Level)dcaillouet User rank is Sergeant (500 - 2000 Reputation Level)dcaillouet User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 16 h 29 m 5 sec
Reputation Power: 24
Try something like:

SELECT files.file_id, files.filename, COUNT(*)
FROM files INNER JOIN labels
ON files.file_id = labels.file_id
WHERE labels.label IN ('labelone', 'labeltwo')
GROUP BY files.file_id, files.filename
HAVING COUNT(*) = 2

Change your count to equal the number of labels in your IN command: If 5 labels then COUNT(*) = 5

Reply With Quote
  #3  
Old August 12th, 2002, 04:34 AM
beyond cool's Avatar
beyond cool beyond cool is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Location: Netherlands
Posts: 4 beyond cool User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Break down your problem into relevant parts.

1. You don't need 'files' except for the filename. So forget about the 'files' part for the first part.

2. So now, you need to get all files which have a certain label. Looking at your code, you don't need to know which file has which label, you just need the files. So, you use an aggregate query.

select
lbl.file_Id
from
labels lbl (nolock)
group by
lbl.file_Id
where
label = 'labelOne'
or label = 'labelTwo'

This will give you the fileId. Now, you need to know the filename. So try this:

select
fil.fileName,
fil.file_Id
from
files fil (nolock)
inner join
(
select
lbl.file_Id
from
labels lbl (nolock)
group by
lbl.file_Id
where
label = 'labelOne'
or label = 'labelTwo'
) lId (nolock)
on lId.file_Id = fil.file_Id

In SQL 2000, you could also write a function for the label table, have it return a table datatype and then inner join on the new table. This is my preferred method, since it takes a whole lot of illegible coding out of the SP's.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDatabase Management > Problem ANDing many to one relationship


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