The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Databases
> MySQL Help
|
Is a subquery the solution?
Discuss Is a subquery the solution? in the MySQL Help forum on Dev Shed. Is a subquery the solution? MySQL Help forum discussing administration, SQL syntax, and other MySQL-related topics. MySQL is an open-source relational database management system (RDBMS).
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 21st, 2013, 12:59 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 5
Time spent in forums: 3 h 19 m
Reputation Power: 0
|
|
|
Is a subquery the solution?
Hi all,
I am very new to mySQL and hope that somebody can help me.
In 1 of my tables (tblPersons) there are (amongst others) the following fields:
prsID, prsFirstName, prsPrefix, prsLastName, prsCountry, prsBirthDate and prsNatRef.
I need to find out which combinations of prsFirstName, prsPrefix, prsLastName, prsCountry occur more than once.
The (I think) 1st step is fairly easy:
Code:
SELECT prsFirstName, prsPrefix, prsLastName, prsCountry, COUNT(prsLastName)
FROM tblPersons
WHERE COUNT(prsLastName) > 1
GROUP BY prsFirstName, prsPrefix, prsLastName, prsCountry
This (part of the) query gives me all names that occur more than once.
The next step is to get the remaining fields connected to these records.
Is it possible to do this with 1 query?
Might using a subquery be the answer?And how should I do that?
Thanks in advance
Hein
|

January 21st, 2013, 02:36 PM
|
 |
SQL Consultant
|
|
Join Date: Feb 2003
Location: Toronto Canada
|
|
yes, it's possible to do this with one query, and yes, using the query you posted (with corrections for syntax errors) as a subquery is the answer
Code:
SELECT tblPersons.*
FROM ( SELECT prsFirstName
, prsPrefix
, prsLastName
, prsCountry
FROM tblPersons
GROUP
BY prsFirstName
, prsPrefix
, prsLastName
, prsCountry
HAVING COUNT(*) > 1 ) AS dupes
INNER
JOIN tblPersons.prsFirstName = dupes.prsFirstName
ON tblPersons.prsPrefix = dupes.prsPrefix
AND tblPersons.prsLastName = dupes.prsLastName
AND tblPersons.prsCountry = dupes.prsCountry
|

January 21st, 2013, 02:51 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 5
Time spent in forums: 3 h 19 m
Reputation Power: 0
|
|
|
Brilliant! Thank you Rudy!
I am glad to see that my thought about using a subquery was correct (I have so far no experience using this kind of query but in my search for the answer I came across it and it seemed right).
If I copy and paste your code into my database it gives an error on line 14: JOIN tblPersons.prsFirstName = dupes.prsFirstName
apparently the "." from dupes.prsFirstName causes a problem. It gets red underlined. (mySQL only gives the location of the error, no extra information is given)
Is it possible to correct this?
If I may I would like to ask you another question. Just as a learning experience:
what is the theory or thought behind putting the subquery in the From statement. It looks as if you are creating a temporary table which gets joined to tblPersons in the last bit of your code?
Anyway: hopefully the dupes.prsFirstName can be corrected.
maybe as an extra bit of information: I am using mySQL Workbench
Thanks Hein
|

January 21st, 2013, 02:55 PM
|
 |
SQL Consultant
|
|
Join Date: Feb 2003
Location: Toronto Canada
|
|
AAAACK!! sorry for the syntax error
less haste, more speed
Code:
SELECT tblPersons.*
FROM ( SELECT prsFirstName
, prsPrefix
, prsLastName
, prsCountry
FROM tblPersons
GROUP
BY prsFirstName
, prsPrefix
, prsLastName
, prsCountry
HAVING COUNT(*) > 1 ) AS dupes
INNER
JOIN tblPersons
ON tblPersons.prsFirstName = dupes.prsFirstName
AND tblPersons.prsPrefix = dupes.prsPrefix
AND tblPersons.prsLastName = dupes.prsLastName
AND tblPersons.prsCountry = dupes.prsCountry
Quote: | Originally Posted by Sport what is the theory or thought behind putting the subquery in the From statement. It looks as if you are creating a temporary table which gets joined to tblPersons in the last bit of your code? | exactly right
a subquery in the FROM clause is called a derived table or inline view
it works just like a temporary table, except it isn't really a temporary table

|

January 21st, 2013, 03:09 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 5
Time spent in forums: 3 h 19 m
Reputation Power: 0
|
|
Rudy,
It works perfectly! Thank you, also for the learning experience.
This will improve my queries that I hope to write in the future big-time
Thanks again for your help!
Hein
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|