Unix Technical Forum

join tables: newbie question

This is a discussion on join tables: newbie question within the MySQL forums, part of the Database Server Software category; --> Hi, I have a table STATES ID NAME ---------------------------- 0 AL 1 CA 2 DE 3 WY and a ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-01-2008, 03:49 PM
lbusoni
 
Posts: n/a
Default join tables: newbie question

Hi,
I have a table STATES

ID NAME
----------------------------
0 AL
1 CA
2 DE
3 WY


and a table ROOTS

NAME MOM_STATE_ID DAD_STATE_ID
-------------------------------------------------------------------------------
John 0 0
Jeff 0 1
Jack 1 2
Jim 3 2


What I need is a query to select Name, Mom_state_name and
Dad_state_name

John AL AL
Jeff AL CA
Jack CA DE
Jim WY DE

I'm sorry fro such a boring question and example: I'm sure there is an
appropriate terminology for that, but I'm not expert in SQL at all

Thanks
Lorenzo




Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 03-01-2008, 03:49 PM
Paul Lautman
 
Posts: n/a
Default Re: join tables: newbie question

lbusoni wrote:
> Hi,
> I have a table STATES
>
> ID NAME
> ----------------------------
> 0 AL
> 1 CA
> 2 DE
> 3 WY
>
>
> and a table ROOTS
>
> NAME MOM_STATE_ID DAD_STATE_ID
> -------------------------------------------------------------------------------
> John 0 0
> Jeff 0 1
> Jack 1 2
> Jim 3 2
>
>
> What I need is a query to select Name, Mom_state_name and
> Dad_state_name
>
> John AL AL
> Jeff AL CA
> Jack CA DE
> Jim WY DE
>
> I'm sorry fro such a boring question and example: I'm sure there is an
> appropriate terminology for that, but I'm not expert in SQL at all
>
> Thanks
> Lorenzo


SELECT
r.name,
m.name Mom_State,
d.name Dad_State
FROM roots r
JOIN states m ON r.mom_state_id = m.id
JOIN states d ON r.dad_state_id = d.id


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-01-2008, 06:21 PM
lbusoni
 
Posts: n/a
Default Re: join tables: newbie question

On Mar 1, 11:34 am, "Paul Lautman" <paul.laut...@btinternet.com>
wrote:
> lbusoni wrote:
> > Hi,
> > I have a table STATES

>
> > ID NAME
> > ----------------------------
> > 0 AL
> > 1 CA
> > 2 DE
> > 3 WY

>
> > and a table ROOTS

>
> > NAME MOM_STATE_ID DAD_STATE_ID
> > -------------------------------------------------------------------------------
> > John 0 0
> > Jeff 0 1
> > Jack 1 2
> > Jim 3 2

>
> > What I need is a query to select Name, Mom_state_name and
> > Dad_state_name

>
> > John AL AL
> > Jeff AL CA
> > Jack CA DE
> > Jim WY DE

>
> > I'm sorry fro such a boring question and example: I'm sure there is an
> > appropriate terminology for that, but I'm not expert in SQL at all

>
> > Thanks
> > Lorenzo

>
> SELECT
> r.name,
> m.name Mom_State,
> d.name Dad_State
> FROM roots r
> JOIN states m ON r.mom_state_id = m.id
> JOIN states d ON r.dad_state_id = d.id


Thanks a lot!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 03-04-2008, 07:23 AM
Michael Austin
 
Posts: n/a
Default Re: join tables: newbie question

lbusoni wrote:
> Hi,
> I have a table STATES
>
> ID NAME
> ----------------------------
> 0 AL
> 1 CA
> 2 DE
> 3 WY
>
>
> and a table ROOTS
>
> NAME MOM_STATE_ID DAD_STATE_ID
> -------------------------------------------------------------------------------
> John 0 0
> Jeff 0 1
> Jack 1 2
> Jim 3 2
>
>
> What I need is a query to select Name, Mom_state_name and
> Dad_state_name
>
> John AL AL
> Jeff AL CA
> Jack CA DE
> Jim WY DE
>
> I'm sorry fro such a boring question and example: I'm sure there is an
> appropriate terminology for that, but I'm not expert in SQL at all
>
> Thanks
> Lorenzo
>
>
>
>

hmmm.. looks like a homework question to me...
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 04:31 PM.


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