Hello,
Problem to list 3 tables side by side in one resulting table.
My tables look like these :
HTML Code:
table a table b table c
------ -------- -------
id name id pets id cars
0 name1 0 pet1 0 car1
1 name2 0 pet2 0 car2
1 pet3 0 car3
1 pet4 1 car4
id is common index for the 3 tables
I would like to have this ideal result :
HTML Code:
id name pets cars
0 name1 pet1 car1
. . pet2 car2
. . . car3
1 name2 pet3 .
. . pet4 car4
I know empty cells are not easy to get, so i'll be happy with this result
id name pets cars
0 name1 pet1 car1
0 name1 pet2 car2
0 name1 pet1(or 2) car3
1 name2 pet3 car4
1 name2 pet4 car4
When I do a test on 3 tables with this classic SQL string:
HTML Code:
SELECT n.id, n.name, p.pets, c.cars FROM table1 n
INNER JOIN table2 p ON n.id = p.id
INNER JOIN table3 c ON n.id = c.id
I got the cartesian product of table1, table2 and table3:
id name pets cars
0 name1 pet1 car1
0 name1 pet1 car2
0 name1 pet1 car3
0 name1 pet2 car1
0 name1 pet2 car2
0 name1 pet2 car3
1 name2 pet3 car4
1 name2 pet4 car4
So I have 8 rows instead of 5 because 8 is sum of
1 * 2 * 3 choices for name1
1 * 2 * 1 choices for name2
Of course my question is how to code SQL query. I tried INNER OUTER JOINS
UNION but didn't get what I wish.
Thanks for any help