Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 242
Mohammed Ahmed Avatar asked Dec 17 '12 04:12

Mohammed Ahmed


People also ask

How remove duplicate columns in join?

Method 1: Using drop() function We can join the dataframes using joins like inner join and after this join, we can use the drop method to remove one duplicate column.

How do I prevent duplicate rows from joining multiple tables?

Solution. Select column values in a specific order within rows to make rows with duplicate sets of values identical. Then you can use SELECT DISTINCT to remove duplicates. Alternatively, retrieve rows in such a way that near-duplicates are not even selected.

Does LEFT join keep duplicates?

Again, if we perform a left outer join where date = date, each row from Table 5 will join on to every matching row from Table 4. However, in this case, the join will result in 4 rows of duplicate dates in the joined DataSet (see Table 6).

Why is my join duplicating rows?

In some cases, you need to join tables by multiple columns. In these situations, if you use only one pair of columns, it results in duplicate rows.


2 Answers

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);
like image 95
Tanzeel Kazi Avatar answered Oct 26 '22 06:10

Tanzeel Kazi


The problem can be solved by "USING" keyword.

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

In your case the query will become

SELECT * FROM FAMILY 
  JOIN (SUPERVISOR JOIN CENTER USING(Center_ID)) USING(Supervisor_ID);

The point is Simple, If you have two Tables A(a,b) and B(b,c) then after joining to produce the result in the form of (a,b,c)

Select *
    from A JOIN B USING(b);

will give the Result-Set with three columns(a,b,c)

NOTE : Since I don't know whether we can use multiple params in Using, therefore I made it as subquery.

like image 35
bharatj Avatar answered Oct 26 '22 06:10

bharatj