Join 2 times

[b]myTable[/b]
(1) 3 6
(2) 8 4
(3) 7 2
(4) 5 1

[b]myTable1[/b]
(1) Mary
(2) Jane
(3) Tom
(4) Carol
(5) Jack
(6) Judy
(7) Rudy
(8) Chris

I have 2 tables like the above and I like to produce my target result like the below.

[b]target result[/b]
(1) Tom    Judy
(2) Chris  Carol
(3) Rudy   Jane
(4) Jack   Mary

The code below is one of steps toward the target result.

[b]on the way code[/b]

select myTable.id,name from myTable
left join myTable1 on male=myTable1.id
order by myTable.id

[b]on the way result[/b]

(1) Tom
(2) Chris
(3) Rudy
(4) Jack

The code below is one of trials but failed.

[b]trial code2[/b]

(select myTable.id,name from myTable
left join myTable1 on male=myTable1.id)

UNION

(select myTable.id,name from myTable
left join myTable1 on female=myTable1.id)

order by myTable.id

[b]trial result[/b]

mysql_fetch_array(): supplied argument is not a valid MySQL result resource.

you’ve done this before, joon

you need one query, with two joins, using table aliases

life is like a box of chocolates, you never know which one you’re going to get. over and over again. :slight_smile:

link, please.

The code below seems not to work.

left join myTable1 on male=myTable1.id
left join myTable1 on female=myTable1.id

I guess I have to create one more cyber table (t1) in query …

What does “aliases” here mean in English?

life is like a box of chocolates,
you eat fast your life will critically down with fat, diabetes, or something.
you eat steadily the box will be empty at last.

Table aliases is the use of AS

eg.

Say you were dealing with fixtures for a football league (soccer), there’s always a home team and an away team, the details for each are all kept in a table called “teams” so for the home team the table alias would be done by (in the FROM clause):


teams AS home_team

then in the same query (also in the FROM clause) for the away team the table alias would be done:


teams AS away_team

Off Topic:

Shame you missed that reference. Forrest Gump. Know thou classics :slight_smile:

i am not the keeper of your links

~you~ look it up

hint: October, 2006

example shows a join to the same table twice, using table aliases

i am very tired of you refusing to remember this stuff

please, search all your threads before asking the same question again

ad infinitum, ad nauseum