join tables: newbie question

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


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-01-2008
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




Reply With Quote
  #2 (permalink)  
Old 03-01-2008
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


Reply With Quote
  #3 (permalink)  
Old 03-01-2008
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!
Reply With Quote
  #4 (permalink)  
Old 03-02-2008
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...
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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 03:17 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0