连接具有相同数据的不同列的表

时间:2020-09-28 10:38:41

标签: sql join

我有三个表收集器,销售员和客户,每个表的第一列具有不同的列,但其中的数据相同,如下所示:

客户:

|---------------------|------------------|
|      cust#          |      cust_name   |
|---------------------|------------------|
|          12         |         a        |
|---------------------|------------------|
|          5                    b
|---------------------|------------------|
|          10         |         c        |
|---------------------|------------------|

推销员:

|---------------------|------------------|
|      sal#          |      sales_name   |
|---------------------|------------------|
|          12         |         t        |
|---------------------|------------------|
|          5                    s
|---------------------|------------------|
|          10         |         v        |
|---------------------|------------------|

收藏家:

|---------------------|------------------|
|      coll#         |      coll_name   |
|---------------------|------------------|
|          12         |         r        |
|---------------------|------------------|
|          5                    k
|---------------------|------------------|
|          10         |         z        |
|---------------------|------------------|

我需要知道如何连接这三个表,并为fist列指定一个这样的名称:

|---------------------|------------------|-------------------|----------------|
|      given_name#    |       cust#      |     sales_name         coll_name   
|---------------------|------------------|-------------------|----------------|
|          12         |         a        |        t                   r
|---------------------|------------------|-------------------|----------------|
|          5                    b                 s                   k 
|---------------------|------------------|-------------------|----------------|
|          10         |         c        |        v                   z
|---------------------|------------------|-------------------|----------------|

任何帮助将不胜感激

3 个答案:

答案 0 :(得分:1)

您可以在这些表中使用INNER JOIN

SELECT c.coll# AS given_name#, cs.cust_name AS cust#, s.sales_name, c.coll_name
  FROM collectors c
  JOIN salesman s
    ON c.coll# = sal#
  JOIN customer cs
    ON c.coll# = cs.cust#

P.S。 :如果您的DBMS是MySQL,则包含#字符的列应使用反引号(`)引号,例如

`given_name#` or `sal#` ..etc.

答案 1 :(得分:0)

每个id是否在所有三个表中始终都有一行?如果是这样,您可以只将其中的1列与AS一起使用

SELECT `sal#` as `given_name#`

答案 2 :(得分:0)

尝试一下。

--********************************
if object_id('customer') is not null
begin
    drop table customer
end
go
create table [dbo].[customer](
    [id] [int] identity(1,1) not null,
    [cust] [int] not null,
    [cust_name] [varchar](20) not null)
go
insert into customer (cust, cust_name) values (12, 'a')
insert into customer (cust, cust_name) values (5, 'b')
insert into customer (cust, cust_name) values (10, 'c')
go


if object_id('salesmen') is not null
begin
    drop table salesmen
end
go
create table [dbo].[salesmen](
    [id] [int] identity(1,1) not null,
    [sal] [int] not null,
    [sales_name] [varchar](20) not null
)
go
insert into salesmen (sal, sales_name) values (12, 't')
insert into salesmen (sal, sales_name) values (5, 's')
insert into salesmen (sal, sales_name) values (10, 'v')
go


if object_id('collectors') is not null
begin
    drop table collectors
end
go
create table [dbo].[collectors](
    [id] [int] identity(1,1) not null,
    [coll] [int] not null,
    [coll_name] [varchar](20) not null
)
go
insert into collectors (coll, coll_name) values (12, 'r')
insert into collectors (coll, coll_name) values (5, 'k')
insert into collectors (coll, coll_name) values (10, 'z')
go


--********************************
select dbo.customer.cust 'given_name', dbo.customer.cust_name 'cust', dbo.salesmen.sales_name 'sales_name', dbo.collectors.coll_name 'coll_name'
from dbo.collectors inner join dbo.customer on dbo.collectors.coll = dbo.customer.cust inner join dbo.salesmen on dbo.customer.cust = dbo.salesmen.sal