Google Answers Logo
View Question
 
Q: Need assistance forming SQL statements ( No Answer,   3 Comments )
Question  
Subject: Need assistance forming SQL statements
Category: Computers > Programming
Asked by: oafpokie-ga
List Price: $5.00
Posted: 21 Sep 2006 18:59 PDT
Expires: 21 Oct 2006 18:59 PDT
Question ID: 767467
Here are three sample MySQL tables, their description 
and structure, and the data they contain. My question
follows at the end.  Thanks!


table `pictures`
Description: A simple list of available picture files
NOTE: filenames given are for illustrative purposes only,
so that you can "see" what is in the picture.  In reality
you would not be able to determine the subject from the filename.

ID(int)
filename(varchar)

ID	filename
1	cat.jpg
2	dog.jpg
3	bird.jpg
4	cat_dog.jpg
5	cat_dog_bird.jpg
6	cat2.jpg

table `subjects`
Description: List of possible all possible subjects
ID(int)
subject(varchar)

ID	subject
1	cat
2	dog
3	bird

table `idx_subjects`
Description: An cross-ref list matching the picture to its subject(s)
subjectID(int)
pictureID(int)

subjectID	pictureID
1		1
1		4
1		5
1		6
2		2
2		4
2		5
3		3
3		5

Using these three tables, I need SQL statements returning
filename(s) for these two queries:

Show me picture(s) that have ONLY a cat.
(Expected results: cat.jpg  cat2.jpg)

Show me picture(s) that have ONLY a cat and a dog.
(Expected results: cat_dog.jpg)
Answer  
There is no answer at this time.

Comments  
Subject: Re: Need assistance forming SQL statements
From: simon1604-ga on 22 Sep 2006 05:24 PDT
 
oafpokie, if my understanding is correct :

For Cat only, use

select filename from pictures where ID in
(select pictureID from idx_subjects where subjectID in
 (select ID from subjects where subject = 'cat')
 and subjectID not in 
 (select ID from subjects where subject != 'cat')

For Cat or Dog only, use

select filename from pictures where ID in
(select pictureID from idx_subjects where subjectID in
 (select ID from subjects where subject in ('cat','dog'))
 and subjectID not in 
 (select ID from subjects where subject != 'cat' and subject != 'dog')

Hope this helps
Subject: Re: Need assistance forming SQL statements
From: leovg-ga on 22 Sep 2006 06:14 PDT
 
/* only cats */
SELECT DISTINCT p.filename 
FROM subjects AS s, idx_subjects AS si , pictures AS p
WHERE s.ID = si.subjectID
AND p.ID = si.pictureID
AND s.subject = 'cat'
AND(SELECT count(pictureID) FROM idx_subjects WHERE pictureID=p.ID)=1

/* only cats and dogs */
SELECT DISTINCT p.filename
FROM subjects AS s, idx_subjects AS si , pictures AS p
WHERE 
	s.ID = si.subjectID
	AND p.ID = si.pictureID
	AND (s.subject='cat' OR s.subject='dog' ) 
	AND si.pictureID IN (
		SELECT DISTINCT pictureID
		FROM `idx_subjects`
		GROUP BY pictureID
		HAVING count(pictureID) = 2
	)
GROUP BY p.filename
HAVING count(p.filename)=2
Subject: Re: Need assistance forming SQL statements
From: oafpokie-ga on 24 Sep 2006 16:31 PDT
 
Thanks for the help!

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy