SQL Basics

 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me



Go Back   Tutorialized ForumsDatabasesSQL Basics

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 Tutorialized Forums Sponsor:
  #1  
Old December 9th, 2012, 02:31 AM
jd254 jd254 is offline
Registered User
Tutorialized Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 2 jd254 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 m 28 sec
Reputation Power: 0
Matching Query Results inside another Query Results

TableA(AID, A1)
TableB(BID, AID)

Code:
SELECT AID FROM TABLEA WHERE A1 = 1;

SELECT B.BID, A.AID FROM AID A, BID B WHERE A.AID = B.BID AND A1 = 1;


The first code above will give a list. (Ex. 1,2,3,6) I need to know if any BID have an AID list that at least have all the results of AID from the first code (Ex. 1, 2, 3, 4, 5, 6 GOOD) (Ex. 1, 2, 3, 5 BAD) I ignored the BID in my examples.

Basically, each AID is either positive or negative for A1. Each BID is either positive or negative with each AID. I need to know which BID is positive for ALL of "the positive AID."

To make it easier to digest. TableA are movies. Each movie have an ID. Some movies are horror films. TableB are users. Which users saw ALL the horror movies?

Any suggestions?

Reply With Quote
  #2  
Old December 9th, 2012, 01:32 PM
jd254 jd254 is offline
Registered User
Tutorialized Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 2 jd254 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 m 28 sec
Reputation Power: 0
I solved it. All I need was a night of sleep and breakfast.

Code:
SELECT B.BID
FROM TableA A, TableB B 
WHERE A.AID = B.BID AND A.A1 = 1
HAVING COUNT(B.AID) = (
SELECT FROM COUNT(A.AID)
FROM AID A
WHERE A.A1 = 1)
GROUP BY B.BID
ORDER BY B.BID
;


Explanation: I got the total count of Horror films that exists. I compared the User's count of horror filmed they watched to that number. If any user has the same count, they show up.

Reply With Quote
  #3  
Old December 10th, 2012, 09:03 AM
Law2rence Law2rence is offline
Registered User
Tutorialized Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 5 Law2rence User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 m
Reputation Power: 0
Hi,guys.thank you for your sharing




Last edited by Law2rence : December 10th, 2012 at 09:08 AM.

Reply With Quote
Reply

Viewing: Tutorialized ForumsDatabasesSQL Basics > Matching Query Results inside another Query Results


Developer Shed Advertisers and Affiliates


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 | 
  
 

Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.

© 2003-2014 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap