简体   繁体   中英

join tables that have different column with the same data

I have three tables collector, salesmen, and customer the first column for each table has different column but with the same data inside it like this:

customer:

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

salesmen:

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

collectors:

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

and I need to know how to join these three tables and give the fist column a name like this:

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

any help will be appreciated

You can use INNER JOIN among those tables :

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#

PS : If your DBMS is MySQL , then the columns containing # characters should be quoted with backticks(`) such as

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

Does each id always have a row in all three tables? if so you could just use 1 of the columns with a AS

SELECT `sal#` as `given_name#`

try this..

--********************************
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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM