Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
793 views
in Technique[技术] by (71.8m points)

database - MySQL Removing duplicate columns on Left Join, 3 tables

I have three tables, each have a foreign key. When I perform a join, I get duplicate columns.

Given

mysql> describe Family;
+---------------+-------------+------+-----+---------+-------+
| Field         | Type        | Null | Key | Default | Extra |
+---------------+-------------+------+-----+---------+-------+
| HEAD_name     | varchar(45) | NO   | PRI |         |       |
| Family_Size   | int(11)     | NO   |     |         |       |
| Gender        | char(1)     | NO   |     |         |       |
| ID_Number     | int(11)     | NO   |     |         |       |
| DOB           | date        | NO   |     |         |       |
| Supervisor_ID | int(11)     | NO   | MUL |         |       |
+---------------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)

mysql> describe SUPERVISOR;
+-------------------+---------------+------+-----+---------+-------+
| Field             | Type          | Null | Key | Default | Extra |
+-------------------+---------------+------+-----+---------+-------+
| Supervisor_ID     | int(11)       | NO   | PRI |         |       |
| Supervisor_Name   | varchar(45)   | NO   |     |         |       |
| Supervisor_Number | decimal(10,0) | NO   |     |         |       |
| Center_ID         | int(11)       | NO   | MUL |         |       |
+-------------------+---------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

mysql> describe CENTER;
+-----------+-------------+------+-----+---------+-------+
| Field     | Type        | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| Center_ID | int(11)     | NO   | PRI |         |       |
| Location  | varchar(45) | NO   |     |         |       |
+-----------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

My query statement:

SELECT * from Family
   JOIN SUPERVISOR on ( Family.Supervisor_ID = SUPERVISOR.Supervisor_ID)
   JOIN CENTER on (SUPERVISOR.Center_ID = CENTER.Center_ID); 

My objective is to get one row of all the columns from the join without duplicate columns. So what is the SQL statement syntax that I should use?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

By default MySQL will return all columns for all tables if you use *. You will need to explicitly enter column names in your query to retrieve them the way you want. Use the query as follows:

SELECT A.HEAD_name, A.Family_Size, A.Gender, A.ID_Number, A.DOB,
    B.Supervisor_ID, B.Supervisor_Name, B.Supervisor_Number,
    C.Center_ID, C.Location
FROM Family A
JOIN SUPERVISOR B on ( A.Supervisor_ID = B.Supervisor_ID)
JOIN CENTER C on (B.Center_ID = C.Center_ID);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...