gpt4 book ai didi

MySQL 删除 Left Join 上的重复列,3 个表

转载 作者:IT王子 更新时间:2023-10-28 23:46:57 25 4
gpt4 key购买 nike

我有三个表,每个表都有一个外键。当我执行连接时,我得到了重复的列。

给定

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)

我的查询语句:

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

我的目标是从连接中获取所有列中的一行,而没有重复的列。那么我应该使用什么样的 SQL 语句语法呢?

最佳答案

如果您使用 *,默认情况下 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);

关于MySQL 删除 Left Join 上的重复列,3 个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13908249/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com