MySQL join four tables!


 
Thread Tools Search this Thread
Top Forums Programming MySQL join four tables!
# 1  
Old 01-23-2012
MySQL join four tables!

Hello;
I want merge four MySQL tables to get the intersection that have a common field for all of them. Join two tables is fine to me, but my this case is different from common situations and there are not very many discussions about it. Can anybody give me some idea? Thanks a lot!
Here is part of my simplified tables (out of ~30000 rows per table and more than 10 columns of complicate formats, so that I do not want use shell command JOIN or other want to do the job!):
Code:
TableA:
A1   O
A2   P
A3   Q
A4   R
A5   S
A6   T

TableB:
B1   A1
B2   A2
B3   A3
B4   A4
B5   A1
B6   A2

Table C: 
C1   A1
C2   A2
C3   A2
C4   A4
C5   A4
C6   A1

TableD
D1   A1
D2   A2
D3   A3
D4   A4
D5   A5
D6   A1

The output is expected to be:
Code:
B1   A1   C1   A1   D1   A1   O   
B2   A2   C2   A2   D2   A2   P
B4   A4   C4   A4   D4   A4   R

My Code is:

SELECT
B.ID, B.AGI,
C.ID, C.AGI,
D.ID, D.AGI,
A.ID, A.AGI FROM TableB B, TableC C, TableD D, TableA A WHERE
B.AGI = A.ID
AND C.AGI = A.ID
AND D.AGI = A.ID
AND B.AGI = C.AGI
AND D.AGI = C.AGI
AND B.AGI = D.AGI;

But the output is not what I expected:
Code:
+------+------+------+------+------+------+------+------+
| ID   | AGI  | ID   | AGI  | ID   | AGI  | ID   | AGI  |
+------+------+------+------+------+------+------+------+
| B1   | A1   | C1   | A1   | D1   | A1   | A1   | O    |
| B5   | A1   | C1   | A1   | D1   | A1   | A1   | O    |
| B1   | A1   | C6   | A1   | D1   | A1   | A1   | O    |
| B5   | A1   | C6   | A1   | D1   | A1   | A1   | O    |
| B1   | A1   | C1   | A1   | D6   | A1   | A1   | O    |
| B5   | A1   | C1   | A1   | D6   | A1   | A1   | O    |
| B1   | A1   | C6   | A1   | D6   | A1   | A1   | O    |
| B5   | A1   | C6   | A1   | D6   | A1   | A1   | O    |
| B2   | A2   | C2   | A2   | D2   | A2   | A2   | P    |
| B6   | A2   | C2   | A2   | D2   | A2   | A2   | P    |
| B2   | A2   | C3   | A2   | D2   | A2   | A2   | P    |
| B6   | A2   | C3   | A2   | D2   | A2   | A2   | P    |
| B4   | A4   | C4   | A4   | D4   | A4   | A4   | R    |
| B4   | A4   | C5   | A4   | D4   | A4   | A4   | R    |
+------+------+------+------+------+------+------+------+
14 rows in set (0.00 sec)

Can anybody give me some idea? Got lost with the LEFT/ RIGHT/INNER JOINs. Expert suggestion can save me days of scratch. Thanks a lot!
# 2  
Old 01-24-2012
Code:
mysql>
mysql>
mysql> select * from tablea;
+----+-----+
| id | agi |
+----+-----+
| A1 | O   |
| A2 | P   |
| A3 | Q   |
| A4 | R   |
| A5 | S   |
| A6 | T   |
+----+-----+
6 rows in set (0.00 sec)

mysql>
mysql> select * from tableb;
+----+-----+
| id | agi |
+----+-----+
| B1 | A1  |
| B2 | A2  |
| B3 | A3  |
| B4 | A4  |
| B5 | A1  |
| B6 | A2  |
+----+-----+
6 rows in set (0.00 sec)

mysql>
mysql> select * from tablec;
+----+-----+
| id | agi |
+----+-----+
| C1 | A1  |
| C2 | A2  |
| C3 | A2  |
| C4 | A4  |
| C5 | A4  |
| C6 | A1  |
+----+-----+
6 rows in set (0.00 sec)

mysql>
mysql> select * from tabled;
+----+-----+
| id | agi |
+----+-----+
| D1 | A1  |
| D2 | A2  |
| D3 | A3  |
| D4 | A4  |
| D5 | A5  |
| D6 | A1  |
+----+-----+
6 rows in set (0.00 sec)

mysql>
mysql>
mysql> -- query
mysql> select b.id, b.agi,
    ->        c.id, c.agi,
    ->        d.id, d.agi,
    ->        a.agi
    ->   from tablea a,
    ->        (select agi, min(id) as id from tableb group by agi) b,
    ->        (select agi, min(id) as id from tablec group by agi) c,
    ->        (select agi, min(id) as id from tabled group by agi) d
    ->  where a.id = b.agi
    ->    and a.id = c.agi
    ->    and a.id = d.agi;
+------+-----+------+-----+------+-----+-----+
| id   | agi | id   | agi | id   | agi | agi |
+------+-----+------+-----+------+-----+-----+
| B1   | A1  | C1   | A1  | D1   | A1  | O   |
| B2   | A2  | C2   | A2  | D2   | A2  | P   |
| B4   | A4  | C4   | A4  | D4   | A4  | R   |
+------+-----+------+-----+------+-----+-----+
3 rows in set (0.00 sec)

mysql>
mysql>
mysql>

tyler_durden

Last edited by durden_tyler; 01-24-2012 at 09:09 AM..
This User Gave Thanks to durden_tyler For This Post:
# 3  
Old 01-24-2012
I was thinking if it is correct algorithm to just get the three rows instead of 14 rows. Any comments Tyler?
Thank you again!
# 4  
Old 01-24-2012
What other rows, precisely, would you want from it?
# 5  
Old 01-24-2012
What I wanted is to pick up all rows of the three tables (B, C & D) by the common key from col1 of TableA, i.e. A1 ~ A6, if B, C & D share any key of each col2 of B, C & D. The 14 rows output seems to be correct, but with exhaust combination of the four tables.
The fact for this study is to pick up all the common genes of the three organisms with Arabidopsis (model plant) as the reference(key).
At first I was thinking about the technique part to retrieve common rows of four tables (3 tables at least). Now I need to think it over after Tyler's code for my purpose, as I did not take the redundant items (key) in B, C & D although items of col1 in TableA are unique.
Thank you anyway.
# 6  
Old 01-24-2012
Quote:
Originally Posted by yifangt
What I wanted is to pick up all rows of the three tables (B, C & D) by the common key from col1 of TableA, i.e. A1 ~ A6, if B, C & D share any key of each col2 of B, C & D.
...
Not sure I understood that fully, but let's say you had only two tables "tablea" and "tableb". And they had data as follows -

Code:
mysql>
mysql>
mysql> select * from tablea;
+----+-----+
| id | agi |
+----+-----+
| A1 | O   |
+----+-----+
1 row in set (0.00 sec)

mysql>
mysql> select * from tableb;
+----+-----+
| id | agi |
+----+-----+
| B1 | A1  |
| B5 | A1  |
+----+-----+
2 rows in set (0.00 sec)

mysql>
mysql>
mysql>

What would have been your expected output in that case?

tyler_durden
# 7  
Old 01-24-2012
Then the output should be:
Code:
B1 A1 O
B5 A1 O

Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script automation using cron which query's MySQL Tables

What I have: I have a input.sh (script which basically connect to mysql-db and query's multiple tables to write back the output to output1.out file in a directory) note: I need to pass an integer (unique_id = anything b/w 1- 1000) next to the script everytime I run the script which generates... (3 Replies)
Discussion started by: kkpand
3 Replies

2. Shell Programming and Scripting

UPDATE COmmand post comparing 2 columns in 2 mysql tables

my queryis : select distinct m.name, item_count, item from master m join client p on m.name=p.name where item_count = 1 and item > 1; But how should I update them? i used update statetment : Update from client Set item =1 where m.name=p.name and item_count=1 AND item>1 Is this wrong? (1 Reply)
Discussion started by: siya@
1 Replies

3. Shell Programming and Scripting

Show only new and removed records by comparing to MySQL tables

Hello all; I have been really frustrated with finding the correct perl code (and MySql statements) to accomplish what I thought was straight forward...I have tested I don't know how many different codes\suggestions I found on the net without any success...but anyhow let me explain my plight and... (0 Replies)
Discussion started by: gvolpini
0 Replies

4. Programming

Query to join two tables in SQL

Hi, I have 2 tables. Table1 name map_code data_code id aaa 2732C 3333B 10 bbb 1223F 2545v 11 ccc 12 Table2 name map_code data_code id text_description aaa 2732C 3333B 10 description 1 bbb 1223F 2545v 11 ... (3 Replies)
Discussion started by: vanitham
3 Replies

5. Programming

MySQL: Create a relation between two tables.

Hello everybody, I'm having troubles creating a relation between two tables in a MySQL database. Having two tables, being one which contains users information (username, password, user ID, etc) and the other the one which contains transactions information (operation type, user ID of the user who... (2 Replies)
Discussion started by: semash!
2 Replies

6. Web Development

How do you sync tables in mysql between two different servers?

Hi all, I have 2 mysql databases running on two different servers (both can be accessed via ssh to each other). If I have say table ABC on server 1, how can I sync it with table ABC on server 2 if the number of records is different? Both databases are the same structure though server 2 has... (2 Replies)
Discussion started by: muay_tb
2 Replies

7. UNIX and Linux Applications

create 'day' tables based on timestamp in mysql

How would one go about creating 'day' tables based on the timestamp field. I have some 'import' tables which contains data from various days and would like to spilt that data up into 'days' based on the timestamp field in new tables. TABLE_IMPORT1 TABLE_IMPORT2 TABLE_IMPORT3 ... (2 Replies)
Discussion started by: hazno
2 Replies
Login or Register to Ask a Question