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 > Help In Creating A Subquery

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-10-08, 01:04
MaryH MaryH is offline
Registered User
 
Join Date: Oct 2008
Posts: 13
Help In Creating A Subquery

I need to list a dog_id and name for each dog that has been placed two or more times.

I have two tables dog and place.

Dog table looks like this:

Name /dog_id
Star/102
Boxer/201
Daisy/301
Lilly/401
etc...

Place table looks like this:

dog_id/place
102/1
201/2
301/3
102/1
201/3
301/2
201/2
401/2

I need a subquery to check which dog has been placed two or more times.


Any help would be appreciable..
Reply With Quote
  #2 (permalink)  
Old 10-10-08, 03:34
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
This query returns the dog_id from the place table where there is more than one occurance
Code:
SELECT dog_id
FROM   place
GROUP
    BY dog_id
HAVING Count(*) > 1
You can then either JOIN to this as a derived table, or you can use the IN operator.
__________________
George
Twitter | Blog
Reply With Quote
  #3 (permalink)  
Old 10-10-08, 04:47
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,250
MaryH
do yourself a favour, at least make an effort to do your homework before giving up and asking questions here. the reason for that is that in my books you have more chance of learning about SQL/MySQL than just having the answer provided for you. thats not to say NEVER ask questions.. but at least make an attempt, otherwise you are cheating yourself
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #4 (permalink)  
Old 10-10-08, 04:52
MaryH MaryH is offline
Registered User
 
Join Date: Oct 2008
Posts: 13
Quote:
Originally Posted by georgev
This query returns the dog_id from the place table where there is more than one occurance
Code:
SELECT dog_id
FROM   place
GROUP
    BY dog_id
HAVING Count(*) > 1
You can then either JOIN to this as a derived table, or you can use the IN operator.
Thank you for your help 'georgev' you gave me a clue of how to create the query!!
Reply With Quote
  #5 (permalink)  
Old 10-10-08, 04:54
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Mary,

do you understand the solution I have provided?
__________________
George
Twitter | Blog
Reply With Quote
  #6 (permalink)  
Old 10-10-08, 05:01
MaryH MaryH is offline
Registered User
 
Join Date: Oct 2008
Posts: 13
Quote:
Originally Posted by healdem
MaryH
do yourself a favour, at least make an effort to do your homework before giving up and asking questions here. the reason for that is that in my books you have more chance of learning about SQL/MySQL than just having the answer provided for you. thats not to say NEVER ask questions.. but at least make an attempt, otherwise you are cheating yourself
Hi 'healdem', I made several attempts trying to create subquerys. I did not give up doing my work, im getting clues or ideas for creating subqueries.

I've read a textbook about mysql and researched hours. The reason why i'm asking these questions is because these questions in my assign are optional not compulsory.. So in other words im trying to get more knowledge of how to create sql subqueries...
Reply With Quote
  #7 (permalink)  
Old 10-10-08, 05:03
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Do you get extra marks for answering these optional questions?
__________________
George
Twitter | Blog
Reply With Quote
  #8 (permalink)  
Old 10-10-08, 05:07
MaryH MaryH is offline
Registered User
 
Join Date: Oct 2008
Posts: 13
Quote:
Originally Posted by georgev
Mary,

do you understand the solution I have provided?
Hi Georgev

Yes,I do understand that the query you provided lists the dog_id for each dog that has been placed more than one time.

And to get the name of the dog i have to join the dog table on dog_id.
Reply With Quote
  #9 (permalink)  
Old 10-10-08, 05:14
MaryH MaryH is offline
Registered User
 
Join Date: Oct 2008
Posts: 13
Quote:
Originally Posted by georgev
Do you get extra marks for answering these optional questions?
No, This is not a graded assignment. Theres 15 questions which 10 of them are compulsory to complete.
Reply With Quote
  #10 (permalink)  
Old 10-10-08, 05:24
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Ok, for extra credit (from us friendly forum folks ) - can you write a query for the following

List a dog_id and name for each dog that has been placed two or more times in first position.

Go on, give it a go! If you can't manage it, post what you've tried and we'll explain it to you.
No harm in trying eh :-)
__________________
George
Twitter | Blog
Reply With Quote
  #11 (permalink)  
Old 10-10-08, 05:35
MaryH MaryH is offline
Registered User
 
Join Date: Oct 2008
Posts: 13
Quote:
Originally Posted by georgev
Ok, for extra credit (from us friendly forum folks ) - can you write a query for the following

List a dog_id and name for each dog that has been placed two or more times in first position.

Go on, give it a go! If you can't manage it, post what you've tried and we'll explain it to you.
No harm in trying eh :-)

SELECT dog_id,name
FROM dog

JOIN (SELECT dog_id
FROM place
GROUP BY dog_id
HAVING COUNT(place) >=2)
on place.dog_id=dog.dog_id
Reply With Quote
  #12 (permalink)  
Old 10-10-08, 06:13
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
What about the first position part?
__________________
George
Twitter | Blog
Reply With Quote
  #13 (permalink)  
Old 10-10-08, 06:17
MaryH MaryH is offline
Registered User
 
Join Date: Oct 2008
Posts: 13
Quote:
Originally Posted by georgev
What about the first position part?
where place=1 ?
Reply With Quote
  #14 (permalink)  
Old 10-10-08, 06:36
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
full code please
__________________
George
Twitter | Blog
Reply With Quote
  #15 (permalink)  
Old 10-10-08, 06:38
MaryH MaryH is offline
Registered User
 
Join Date: Oct 2008
Posts: 13
SELECT dog_id,name
FROM dog
JOIN (SELECT dog_id
FROM place where place=1
GROUP BY dog_id
HAVING COUNT(place) >=2)
on place.dog_id=dog.dog_id
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