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 > Data Access, Manipulation & Batch Languages > ANSI SQL > List 3 tables

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-15-09, 21:13
smr smr is offline
Registered User
 
Join Date: Nov 2009
Posts: 2
List 3 tables

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
Reply With Quote
  #2 (permalink)  
Old 11-15-09, 21:59
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,084
Code:
SELECT 'a'    AS rowtype
     , id
     , name   AS fld
  FROM table_a
UNION ALL
SELECT 'b'    
     , id
     , pets   
  FROM table_b
UNION ALL
SELECT 'c'    
     , id
     , cars   
  FROM table_c
ORDER
    BY id
     , rowtype
this is the simplest and most efficient query

lay it out for display however you want using your application language or reporting system
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 11-16-09, 04:06
smr smr is offline
Registered User
 
Join Date: Nov 2009
Posts: 2
perfect, perfect, thanks.

I saw that UNION could mix two tables in a 2 columns table, but I didn't see how to know which field comes from which table. Adding the rowtype marker is the trick.
First I got a table with 2 columns and right number of rows, then realised I had to display the rowtype value in a 3rd column to display the marker.
Finally, I just had to add ORDER BY id, rowtype, fld
So PHP code is:
$query= "SELECT 'a' as rowtype
,id
,name as fld
....

$val=mysql_query($query);
while ($myArr=mysql_fetch_array($val)) {
print($myArr['id'].";"$myArr['fld'].";"$myArr['rowtype]."\n");
}
the following the result looks like
id fld rowtype
0 ; name1 ; a
0 ; pet1 ; b


Now UNION is clear for me (I hope)
Thanks again

Last edited by smr; 11-16-09 at 04:16.
Reply With Quote
Reply

Tags
list, multi, tables

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On