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 > Query (dont know how to title this)

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-22-07, 16:37
milos.sakovic milos.sakovic is offline
Registered User
 
Join Date: Jan 2007
Posts: 2
Red face Query (dont know how to title this)

Here is the table

Code:
Place     |Name
-----------------------
Denver    |Jane
Denver    |Marianne
Denver    |Morgan
Boston    |Emma
Boston    |Phill
Boston    |Marianne
Atlanta   |Morgan
Atlanta   |Mark
Atlanta   |Marianne
I want to get everything where Marianne and Morgan both were (Denver and Atlanta). How?!
Reply With Quote
  #2 (permalink)  
Old 01-22-07, 17:46
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,407
Try this:
Code:
SELECT p1.place
FROM places p1 JOIN (SELECT place
                     FROM places
                     WHERE name = 'Morgan') p2 ON (p1.place = p2.place)
WHERE p1.name = 'Marianne';
Reply With Quote
  #3 (permalink)  
Old 01-22-07, 18:19
dbmab dbmab is offline
Registered User
 
Join Date: Apr 2006
Location: Denver, Co. USA
Posts: 240
I believe the following should work as well -
Code:
SELECT place, count(*) as cnt FROM your_table WHERE name = 'marianne' OR name = 'morgan' GROUP BY place HAVING cnt = 2
Reply With Quote
  #4 (permalink)  
Old 01-22-07, 19:38
milos.sakovic milos.sakovic is offline
Registered User
 
Join Date: Jan 2007
Posts: 2
Thank you

Thank you both!

However, shammat`s solution was significantly faster!

It took 0.0314s vs 0.5130s (dbmab`s solution) on 250k records tables.

Thank you, again.
Reply With Quote
  #5 (permalink)  
Old 01-24-07, 05:35
tholder tholder is offline
Registered User
 
Join Date: Dec 2006
Location: Bristol, England
Posts: 7
interesting...

Wow... I find that quite suprising. Interesting

What were you doing to measure the performance?
Reply With Quote
  #6 (permalink)  
Old 01-24-07, 05:46
tholder tholder is offline
Registered User
 
Join Date: Dec 2006
Location: Bristol, England
Posts: 7
Just looked a bit closer at the dbmab solution and it's not actually correct as it's only checking they were both in two places not necessarily the same places and it's not constrained to Denver and Atlanta.

I think this would work:

SELECT Name
FROM YourTable
WHERE (Name = 'marianne' OR Name = 'morgan') And (Place = 'Denver' Or Place = 'Atlanta')
GROUP BY Name
HAVING COUNT(Name) = 2

This could perform slightly better if the name field is indexed. Personally I find group by easier to understand than joinging a table to itself... but if the performance gap remains I'd go with the join
Reply With Quote
  #7 (permalink)  
Old 01-24-07, 06:27
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
tom, i don't agree with your solution

the original question was to "get everything where Marianne and Morgan both were"

you wouldn't know in advance that it was only Denver and Atlanta -- maybe they both were in Kalamazoo too
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #8 (permalink)  
Old 01-24-07, 07:08
tholder tholder is offline
Registered User
 
Join Date: Dec 2006
Location: Bristol, England
Posts: 7
Sorry in that case I misinterpreted the question as it says (Denver and Atlanta).

It's fun doing SQL without the database - gets the brain ticking
Reply With Quote
  #9 (permalink)  
Old 01-24-07, 10:35
guelphdad guelphdad is offline
Registered User
 
Join Date: Mar 2004
Posts: 440
dbmab's solution scales better:
Code:
SELECT place, count(*) as cnt 
FROM your_table 
WHERE name = 'marianne' OR name = 'morgan' 
GROUP BY place 
HAVING cnt = 2
note that the names can be an IN list instead, making it easier to read with more names:

Code:
SELECT place, count(*) as cnt 
FROM your_table 
WHERE name IN ('marianne','morgan')
GROUP BY place 
HAVING cnt = 2
so if you want to find which cities have been visited by four friends, you expand the name list and change the count:

Code:
SELECT place, count(*) as cnt 
FROM your_table 
WHERE name IN ('larry','curly','moe','shemp')
GROUP BY place 
HAVING cnt = 4
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