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?
我的目標是從連接中獲取一行沒有重復列。那么我應該使用的SQL語句語法是什么?
12
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:
默認情況下,如果使用*,MySQL將返回所有表的所有列。您需要在查詢中明確輸入列名,以便按照您希望的方式檢索它們。使用查詢如下:
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);
5
The problem can be solved by "USING" keyword.
問題可以通過“USING”關鍵字來解決。
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)
這一點很簡單,如果你有兩個表A(a,b)和B(b,c),那么在連接后以(a,b,c)的形式產生結果
Select *
from A JOIN B USING(b);
will give the Result-Set with three columns(a,b,c)
將為結果集提供三列(a,b,c)
NOTE : Since I don't know whether we can use multiple params in Using, therefore I made it as subquery.
注意:因為我不知道我們是否可以在使用中使用多個參數,因此我將其作為子查詢。
1
You are not getting duplicate columns, what you are really getting is the Supervisor_ID column from table Family (that is Family.Supervisor_ID) and Supervisor_ID from table Supervisor (that is Supervisor.Supervisor_ID) but to your result set, you will see both as Supervisor_ID, and that is why you think they are duplicated. The same will happen with Center_iD.
你沒有得到重復的列,你真正得到的是表Family的Supervisor_ID列(即Family.Supervisor_ID)和表Supervisor的Supervisor_ID(即Supervisor.Supervisor_ID),但是對於你的結果集,你會看到兩者都是Supervisor_ID ,這就是為什么你認為它們是重復的。 Center_iD也會發生同樣的情況。
The solution is to specify the fields that you need from each table, and decide if you need to get the Supervisor_ID and Center_ID and which table to get it from.
解決方案是從每個表中指定所需的字段,並確定是否需要獲取Supervisor_ID和Center_ID以及從哪個表獲取它。
0
According to oreilly,
根據oreilly,
Look for an explain plan.
You can do a _select x from y inner join.... where w=z ... this is just an example
尋找解釋計划。你可以從y inner join做一個_select x,其中w = z ......這只是一個例子
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/12/17/725ea14bfc7e01494e0fcdb81d91cac7.html。