ORA-00904:此实例中的标识符无效

时间:2017-03-06 05:23:41

标签: sql oracle date

这是我的解决方案,我想用薪水显示3个表中的所有信息,当我把工资放到最后时,无论我怎么做,我还是错了。有人帮我解决了。我不知道如何只用15行显示工资。

SELECT Bld.id,C.code,M.FIRST_NAME,M.LAST_NAME,Bld.Address,M.ADDRESS,D.DOB,S.SALARY
    from HW1_PERSON M
    inner join HW1_BUILDING Bld
    ON M.id = Bld.id
    INNER JOIN HW1_PERSON M 
    ON Bld.id = M.id
    INNER JOIN HW1_PERSON M 
    ON M.id = Bld.id
    Inner JOIN HW1_BUILDING Bld
    ON Bld.id = M.id
    INNER JOIN HW1_BUILDING C
    ON M.id = C.id
    INNER JOIN HW1_PERSON D
    ON M.id = D.id
    UNION ALL
    SELECT Bld.id,C.code,M.FIRST_NAME,M.LAST_NAME,Bld.Address,M.ADDRESS,D.DOB,S.SALARY FROM HW1_STAFF S
    where S.SALARY = NULL
    ;

以下是表格

CREATE TABLE HW1_PERSON (
     ID INT NOT NULL,
     FIRST_NAME VARCHAR(32) NOT NULL,
     LAST_NAME VARCHAR(32) NOT NULL,
     DOB DATE,
     ADDRESS VARCHAR2(32),
     CONSTRAINT PK_HW1_PERSON_ID PRIMARY KEY (ID));

CREATE TABLE HW1_STAFF (
     SALARY DOUBLE PRECISION NOT NULL,
     START_DATE DATE NOT NULL,
     END_DATE DATE DEFAULT NULL,
     EMP_NUM VARCHAR(5) NOT NULL UNIQUE,
     PERSON_ID INT NOT NULL,
     CONSTRAINT PK_HW1_STAFF PRIMARY KEY (PERSON_ID),
     CONSTRAINT FK_STAFF_PERSON FOREIGN KEY(PERSON_ID) REFERENCES HW1_PERSON(ID));

CREATE TABLE HW1_BUILDING (
     ID INT NOT NULL,
     CODE VARCHAR2(5) NOT NULL UNIQUE,
     NAME VARCHAR2(32) NOT NULL,
     ADDRESS VARCHAR2(32),
     MANAGER_ID INT,
     CONSTRAINT PK_HW1_BUILDING PRIMARY KEY (ID),
     CONSTRAINT FK_BUILDING_STAFF FOREIGN KEY(MANAGER_ID) REFERENCES HW1_STAFF(PERSON_ID));

CREATE TABLE HW1_EMPLOYEE (
     ID INT NOT NULL,
     PERSON_ID INT,
     BUILDING_ID INT,
     CONSTRAINT PK_HW1_EMPLOYEE PRIMARY KEY (PERSON_ID,BUILDING_ID),
     CONSTRAINT FK_STAFF_ID FOREIGN KEY(PERSON_ID) REFERENCES HW1_STAFF(PERSON_ID),
     CONSTRAINT FK_BUILDING_ID FOREIGN KEY(BUILDING_ID) REFERENCES HW1_BUILDING(ID));

数据示例

INSERT INTO hw1_person (id, first_name, last_name, dob, address) VALUES (1, 'fname11', 'lname11', to_date('7/27/1990', 'MM/DD/YYYY'), '527 15th Av CT');
INSERT INTO hw1_person (id, first_name, last_name, dob, address) VALUES (2, 'fname12', 'lname12', to_date('12/5/1966', 'MM/DD/YYYY'), '254 11th St CT');


INSERT INTO hw1_staff (person_id, emp_num, start_date, end_date, salary) VALUES (1, 'WZIAE', to_date('7/1/1965', 'MM/DD/YYYY'), null,70000);
INSERT INTO hw1_staff (person_id, emp_num, start_date, end_date, salary) VALUES (2, 'EWEMU', to_date('11/18/1980', 'MM/DD/YYYY'), to_date('12/26/1970', 'MM/DD/YYYY'),80000);
INSERT INTO hw1_staff (person_id, emp_num, start_date, end_date, salary) VALUES (25, '00001', to_date('5/21/1994', 'MM/DD/YYYY'), null,0);
INSERT INTO hw1_staff (person_id, emp_num, start_date, end_date, salary) VALUES (26, '00002', to_date('7/19/1985', 'MM/DD/YYYY'), null,0);

2 个答案:

答案 0 :(得分:0)

SELECT Bld.id,C.code,M.FIRST_NAME,M.LAST_NAME,Bld.Address,M.ADDRESS,D.DOB, '0' AS S.SALARY
    from HW1_PERSON M
    inner join HW1_BUILDING Bld
    ON M.id = Bld.id
    INNER JOIN HW1_PERSON M 
    ON Bld.id = M.id
    INNER JOIN HW1_PERSON M 
    ON M.id = Bld.id
    Inner JOIN HW1_BUILDING Bld
    ON Bld.id = M.id
    INNER JOIN HW1_BUILDING C
    ON M.id = C.id
    INNER JOIN HW1_PERSON D
    ON M.id = D.id
    UNION ALL
    SELECT Bld.id,C.code,M.FIRST_NAME,M.LAST_NAME,Bld.Address,M.ADDRESS,D.DOB,S.SALARY FROM HW1_STAFF S
    where S.SALARY = NULL
    ;

我的第一个查询列不存在S.SALARY所以设置默认值是' 0' 0或者''

答案 1 :(得分:0)

首先,你在这里做了一些不必要的连接。

你写了

from HW1_PERSON M
inner join HW1_BUILDING Bld
ON M.id = Bld.id
INNER JOIN HW1_PERSON M 
ON Bld.id = M.id
INNER JOIN HW1_PERSON M 
ON M.id = Bld.id
Inner JOIN HW1_BUILDING Bld
ON Bld.id = M.id
INNER JOIN HW1_BUILDING C
ON M.id = C.id
INNER JOIN HW1_PERSON D
ON M.id = D.id

但是,如果你写过这个,那就足够了

from HW1_PERSON M
inner join HW1_BUILDING Bld
ON M.id = Bld.id
INNER JOIN HW1_BUILDING C
ON M.id = C.id

此外,您已使用MD作为同一个表HW1_PERSON的别名 - 这会在执行查询时导致错误

其次,正如评论中所述,您的第一个查询不包含S.SALARY中的HW1_STAFF列。从您的表结构中,我认为您可以在第一个查询中执行此操作。

SELECT Bld.id,C.code,M.FIRST_NAME,M.LAST_NAME,Bld.Address,M.ADDRESS,M.DOB,S.SALARY
from HW1_PERSON M
inner join HW1_BUILDING Bld
ON M.id = Bld.id
INNER JOIN HW1_BUILDING C
ON M.id = C.id
INNER JOIN HW1_STAFF S
ON S.PERSON_ID = M.id

此外,在您的第二个查询中,您在NULL中搜索了S.SALARY,但根据您的HW1_STAFF表的定义,该列不会为空。因此,在第二个查询中,您将不会得到任何结果。也许你应该改变那个查询 - 就像这样

SELECT Bld.id,C.code,M.FIRST_NAME,M.LAST_NAME,Bld.Address,M.ADDRESS,D.DOB,S.SALARY FROM HW1_STAFF S
where S.END_DATE = NULL

然后,整个查询看起来像这样

SELECT Bld.id,C.code,M.FIRST_NAME,M.LAST_NAME,Bld.Address,M.ADDRESS,M.DOB,S.SALARY
from HW1_PERSON M
inner join HW1_BUILDING Bld
ON M.id = Bld.id
INNER JOIN HW1_BUILDING C
ON M.id = C.id
INNER JOIN HW1_STAFF S
ON S.PERSON_ID = M.id
UNION ALL
SELECT Bld.id,C.code,M.FIRST_NAME,M.LAST_NAME,Bld.Address,M.ADDRESS,M.DOB,S.SALARY FROM HW1_STAFF S
where S.END_DATE = NULL
;