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 > need help with query

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-24-03, 21:43
bitar bitar is offline
Registered User
 
Join Date: Dec 2003
Posts: 11
need help with query

i'm actually using postgreSQL but sql language doesnt change for mySQL, right? and nobody replied over at the postgre section so i tought maybe somebody here would

i'll describe my problem with a simplier example
lets say i have the following tables:

create table names(
id serial PRIMARY KEY,
name text
);

create table x(
x1 integer REFERENCES names (id),
x2 integer REFERENCES names (id),
x3 integer REFERENCES names (id)
);

so... my names table is populated with

id | name
-----------
1 | name1
2 | name2
3 | name3
4 | name4
5 | name5

and my x table contains the following:

x1 | x2 | x3
--------------
2 | 3 | 5
1 | 4 | 2


now, what i need is to make a query similar to:
select name from names,x where x.x1=names.id;


but the tricky thing is that i need to list the 3 names
for example for the first row of the x table (2, 3,5), i would need to get a list looking like this:

name | name | name
----------------------------
name2 | name3 | name5




i have tried a lot of combinations of queries but havent succeded, the closest i get is

name
--------
name2
name3
name5


if this is not possible let me know so i can stop trying
Reply With Quote
  #2 (permalink)  
Old 12-24-03, 23:53
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
hmmm, i missed your post in the postgresql forum, i would've responded earlier, must have been the [hic] christmas spirits, eh
Code:
select n1.name as name1
     , n2.name as name2
     , n3.name as name3
  from x
inner
  join names as n1
    on x1 = n1.id   
inner
  join names as n2
    on x2 = n2.id      
inner
  join names as n3
    on x3 = n3.id
rudy
http://r937.com/
Reply With Quote
  #3 (permalink)  
Old 12-25-03, 08:04
bitar bitar is offline
Registered User
 
Join Date: Dec 2003
Posts: 11
thanks!!!
i would have never come up with that
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