Unix Technical Forum

Best way to use subquery?

This is a discussion on Best way to use subquery? within the MySQL forums, part of the Database Server Software category; --> There are two tables. Person and PersonLocales. Person has PersonID, LastName, FirstName and other columns. PersonLocale has PersonID, City, ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 09:40 AM
greg.scharlemann@gmail.com
 
Posts: n/a
Default Best way to use subquery?

There are two tables. Person and PersonLocales.
Person has PersonID, LastName, FirstName and other columns.
PersonLocale has PersonID, City, State.

A Person can have an unlimited number of PersonLocales as long as each
row in PersonLocale for the defined PersonID is unique. I'm trying to
return all of the PersonID's in a specific state ordered by the
LastName, but I can't quite get my subquery to work.

SELECT distinct PersonLocales.PersonID FROM PersonLocales WHERE
State = 'NC' AND PersonLocales.PersonID = (
SELECT Person.PersonID FROM Person
where PersonLocales.PersonID = Person.PersonID
ORDER BY Person.LastName
)
LIMIT 0 , 30

The problem I am getting is the above query returns a row containing a
PersonID for each entry in the PersonLocales.

1. How do I eliminate the duplicate rows? My distinct statement doesn't
seem to do the trick.
2. Is there a better way to do this query from a database perspective?

Thanks for your help.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 09:40 AM
strawberry
 
Posts: n/a
Default Re: Best way to use subquery?

Don't use a subquery - and don't have duplicate rows!

Use a JOIN instead, something like (untested):

SELECT P.PersonID FROM PersonLocale PL
LEFT JOIN Person P ON P.PersonID = PL.PersonID
WHERE State = 'NC'
ORDER BY LastName,FirstName

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 09:40 AM
greg.scharlemann@gmail.com
 
Posts: n/a
Default Re: Best way to use subquery?


strawberry wrote:
>
> SELECT P.PersonID FROM PersonLocale PL
> LEFT JOIN Person P ON P.PersonID = PL.PersonID
> WHERE State = 'NC'
> ORDER BY LastName,FirstName


Thanks, but the first two records that I get a NULL and I none of the
columns that I'm quering on have a NULL value. Any idea why?

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-28-2008, 09:40 AM
greg.scharlemann@gmail.com
 
Posts: n/a
Default Re: Best way to use subquery?


greg.scharlem...@gmail.com wrote:
> strawberry wrote:
> >
> > SELECT P.PersonID FROM PersonLocale PL
> > LEFT JOIN Person P ON P.PersonID = PL.PersonID
> > WHERE State = 'NC'
> > ORDER BY LastName,FirstName

>
> Thanks, but the first two records that I get a NULL and I none of the
> columns that I'm quering on have a NULL value. Any idea why?


I think it's showing an AgentID that is all zeros (00000000) as NULL.
Not sure why it's listing it twice though.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-28-2008, 09:40 AM
greg.scharlemann@gmail.com
 
Posts: n/a
Default Re: Best way to use subquery?


greg.scharlemann@gmail.com wrote:
> greg.scharlem...@gmail.com wrote:
> > strawberry wrote:
> > >
> > > SELECT P.PersonID FROM PersonLocale PL
> > > LEFT JOIN Person P ON P.PersonID = PL.PersonID
> > > WHERE State = 'NC'
> > > ORDER BY LastName,FirstName

> >
> > Thanks, but the first two records that I get a NULL and I none of the
> > columns that I'm quering on have a NULL value. Any idea why?

>
> I think it's showing an AgentID that is all zeros (00000000) as NULL.
> Not sure why it's listing it twice though.


I got it. My tables were not correctly aligned. I had deleted a Person
but did not remove the PersonID from the PersonLocale table.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 03:10 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com