0

我正在尝试运行一个查询,我将从具有多个搜索选项的 from 转换为 mySQL 查询。这是我在 myPHPadmin 中测试的查询

SELECT Fname, Minit, Lname, Address, Hourly, Sname, Saddress, Dno
FROM (SELECT * 
FROM EMPLOYEE, DEPARTMENT, LOCATION
WHERE EMPLOYEE.Dno = DEPARTMENT.Dnumber AND
LOCATION.Store_num = DEPARTMENT.Store_num AND
(Fname='100' OR Lname='100' 
OR LOCATION.Store_num ='100' OR Dno='100')) X

所有的 OR 语句都将变成 '?' 在 mySQL 中,所以现在我使用 100,它只适用于 Store_num。因此,这应该会列出在该商店工作的员工名单。

但是,我得到的错误是:#1060 - Duplicate column name 'Mgr_ssn'

这是我的表,不知道为什么它们没有正确合并。

CREATE TABLE EMPLOYEE
(   Fname       VARCHAR(15) not null,
    Minit       CHAR,
    Lname       VARCHAR(15) not null,
    Ssn         CHAR(9)     not null,
    Bdate       DATE,
    Address     VARCHAR(30), 
    Hourly      DECIMAL(5,2), 
    Mgr_ssn     CHAR(9),
    Dno         INT     not null,
    Start_date  DATE,
    Phone       CHAR(10),   
    PRIMARY KEY (Ssn),
    FOREIGN KEY (Mgr_ssn) REFERENCES EMPLOYEE(Ssn), 
    FOREIGN KEY (Dno) REFERENCES DEPARTMENT(Dnumber) );

CREATE TABLE DEPARTMENT
(   Dname       VARCHAR(15) not null,
    Dnumber     INT         not null,
    Mgr_ssn     CHAR(9)     not null,
    Mgr_start_date  DATE,
    Phone       CHAR(10),
    Store_num   INT     not null,
    PRIMARY KEY (Dnumber, Store_num),
    UNIQUE (Dname),
    FOREIGN KEY (Mgr_ssn) REFERENCES EMPLOYEE(Ssn),
    FOREIGN KEY (Store_num) REFERENCES LOCATION(Store_num) 
);

CREATE TABLE LOCATION
(   Store_num   INT         NOT NULL,
    Sname       VARCHAR(15)     NOT NULL, 
    Saddress    VARCHAR(30) NOT NULL,
    PRIMARY KEY (Store_num), 
    UNIQUE (Saddress)
);
4

2 回答 2

1

问题就在这里

SELECT *  FROM EMPLOYEE, DEPARTMENT, LOCATION

EMPLOYEEDEPARTMENT有两Mgr_ssn列。您需要指定来自的表Mgr_ssn,例如

SELECT EMPLOYEE.Mgr_ssn AS `EMP_Mgr_ssn`,
       DEPARTMENT.Mgr_ssn AS `DEP_Mgr_ssn`,
       .....
FROM EMPLOYEE, DEPARTMENT, LOCATION

analias也可以帮助您解决它。

所以完整的查询将是

SELECT Fname, Minit, Lname, Address, Hourly, Sname, Saddress, Dno
FROM 
(
    SELECT LOCATION.*,
           Employee.Mgr_ssn AS `EMP_Mgr_ssn`,
           DEPARTMENT.Mgr_ssn AS `DEP_Mgr_ssn`,
           Employee.Fname,
           Employee.Minit,
           Employee.Lname,
           Employee.Address,
           Employee.Hourly,Employee.Dno
    FROM EMPLOYEE, DEPARTMENT, LOCATION
    WHERE EMPLOYEE.Dno = DEPARTMENT.Dnumber AND
          LOCATION.Store_num = DEPARTMENT.Store_num AND
        (Fname='100' OR Lname='100' 
         OR LOCATION.Store_num ='100' OR Dno='100'
        )
) X

但是这个查询也可以工作,因为你没有Mgr_ssn

SELECT Fname, Minit, Lname, Address, Hourly, Sname, Saddress, Dno
FROM 
(
    SELECT LOCATION.*,
           Employee.Fname,
           Employee.Minit,
           Employee.Lname,
           Employee.Address,
           Employee.Hourly,Employee.Dno
    FROM EMPLOYEE, DEPARTMENT, LOCATION
    WHERE EMPLOYEE.Dno = DEPARTMENT.Dnumber AND
          LOCATION.Store_num = DEPARTMENT.Store_num AND
        (Fname='100' OR Lname='100' 
         OR LOCATION.Store_num ='100' OR Dno='100'
        )
) X
于 2012-11-26T05:20:41.070 回答
0

您的两个表包含“Mgr_ssn”列。要解决上述问题,请使用以下查询更改其中一列的名称

选择employee.Mgr_ssn 作为'give_any_name' .....

于 2012-11-26T05:21:16.927 回答