If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Is this possible?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-04-11, 16:29
damiantaylor damiantaylor is offline
Registered User
 
Join Date: Mar 2011
Posts: 2
Is this possible?

Hi all,
I'm still pretty inexperienced with SQL so this might be a dumb or easy question, but here goes....

Is it possible to return a value from a query when the result is based over multiple rows?

For example, I have a table (TABLE_A) containing the following rows:
Code:
ID        VALUE
1           6
1           7
2           6
2           8
3           7
3           9
All I know for my select is the VALUE must contain both 6 AND 7.

So, I need a query that will return ID 1 only as that is the only one that has a value of both 6 and 7.

Is this possible?
Reply With Quote
  #2 (permalink)  
Old 03-04-11, 17:10
dav1mo dav1mo is offline
Registered User
 
Join Date: Dec 2007
Location: Richmond, VA
Posts: 782
Code:
select ID from table_a a
where exists (select 1 from table_a b
                   where a.ID = b.ID
                       and b.value = 6)
   and exists (select 1 from table_a c
                   where a.ID = c.ID
                       and c.value = 7)
Reply With Quote
  #3 (permalink)  
Old 03-04-11, 17:15
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,535
Code:
SELECT id 
  FROM table_a 
 WHERE value IN ( 6,7 )
GROUP
    BY id
HAVING COUNT(*) = 2
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 03-05-11, 06:04
damiantaylor damiantaylor is offline
Registered User
 
Join Date: Mar 2011
Posts: 2
seson museum

Thanks guys!
I especially like the last answer as it's easier to expand if there are 3 criteria
Reply With Quote
  #5 (permalink)  
Old 03-05-11, 06:11
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,535
Quote:
Originally Posted by damiantaylor View Post
... it's easier to expand if there are 3 criteria
and ~much~ easier if you need to do something like "has at least 3 of the following 5" ...
Code:
SELECT id 
  FROM table_a 
 WHERE value IN ( 6,7,8,9,37 )
GROUP
    BY id
HAVING COUNT(*) >= 3
sure as heck can't do this one easily with joins
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 03-06-11, 08:29
cures cures is offline
Registered User
 
Join Date: Mar 2011
Location: Malaysia
Posts: 1
This a great info for newbie. Thanks
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On