不明确的列名称查询

时间:2016-01-02 06:59:24

标签: mysql

当我尝试在salesfacts表中进行内部联接时,我总是会得到一个模糊的错误:

Msg 209, Level 16, State 1, Line 36
Ambiguous column name 'officeCode'.
Msg 209, Level 16, State 1, Line 32
Ambiguous column name 'officeCode'.

任何人都可以帮我解决这个问题吗?非常感谢。

要插入SalesFacts表的代码

truncate table SalesFacts

INSERT INTO Assignment1SS..SalesFacts
(ProductCode, CustomerNumber, EmployeeNumber, officeCode, buyPrice, MSRP, quantityOrdered)

SELECT 

p.ProductCode, 
c.CustomerNumber, 
e.EmployeeNumber, 
y.officeCode, 
p.buyPrice, 
p.MSRP, 
od.quantityOrdered

FROM
Assignemnt1..[OrderDetails] od INNER JOIN Assignemnt1..[Orders] o
ON od.orderNumber = o.orderNumber

INNER JOIN Assignment1SS..[ProductSS] p 
ON od.productCode = p.productCode

INNER JOIN Assignment1SS..[CustomersSS] c
ON od.OrderNumber = c.CustomerNumber

INNER JOIN Assignment1SS..[EmployeeSS] e
ON c.SalesRepEmployeeNumber = e.EmployeeNumber

INNER JOIN

(select [EmployeeNumber], [officeCode]
from Assignemnt1..[Employees] e
inner join Assignemnt1..[Offices] y
on e.officeCode = y.officeCode
group by [EmployeeNumber],[officeCode]) y

on e.EmployeeNumber = c.salesRepEmployeeNumber

办公室表

Create Table Offices (
officeCode varchar (10) NOT NULL,
City varchar (50) NOT NULL,
Phone varchar (50) NOT NULL,
AddressLine1 varchar (50) NOT NULL,
AddressLine2 varchar (50) NULL,
State varchar (50) NULL,
Country varchar (50) NOT NULL,
PostalCode varchar (15) NOT NULL,
Territory varchar (10) NOT NULL,
PRIMARY KEY (OfficeCode));

员工表

Create Table Employees (
EmployeeNumber int NOT NULL,
LastName varchar (50) NOT NULL,
FirstName varchar (50) NOT NULL,
Extension varchar (10) NOT NULL,
Email varchar (100) NOT NULL,
officeCode varchar (10) FOREIGN KEY REFERENCES Offices(officeCode) NOT NULL,
ReportsTo int NULL,
JobTitle varchar (50),
PRIMARY KEY (EmployeeNumber));

1 个答案:

答案 0 :(得分:0)

试试这种方式

    SELECT 

    p.ProductCode, 
    c.CustomerNumber, 
    e.EmployeeNumber, 
    y.officeCode, 
    p.buyPrice, 
    p.MSRP, 
    od.quantityOrdered

    FROM
    Assignemnt1..[OrderDetails] od INNER JOIN Assignemnt1..[Orders] o
    ON od.orderNumber = o.orderNumber

    INNER JOIN Assignment1SS..[ProductSS] p 
    ON od.productCode = p.productCode

    INNER JOIN Assignment1SS..[CustomersSS] c
    ON od.OrderNumber = c.CustomerNumber

    INNER JOIN Assignment1SS..[EmployeeSS] e
    ON c.SalesRepEmployeeNumber = e.EmployeeNumber

    INNER JOIN

    (select [EmployeeNumber], y.[officeCode]
    from Assignemnt1..[Employees] e
    inner join Assignemnt1..[Offices] y
    on e.officeCode = y.officeCode
    group by [EmployeeNumber],y.officeCode) y
on e.EmployeeNumber = c.salesRepEmployeeNumber