简体   繁体   中英

Invalid Identifier in query

I keep getting an "invalid identifier" on DepartmentName when I enter this code:

SELECT LastName "Last Name", Gender "Gender"
FROM Employee
WHERE DepartmentName =
(SELECT DepartmentName FROM Department WHERE DepartmentName = 'Radiology');

Any help is appreciated!

If you have double quotes in your column names (Say "DepartmentName") while table creation then you should have column with double quotes in select query also.

If you use Where DepartmentName you will get an error ora-00904-invalid-identifier

So to avoid error, you have to use WHERE "DepartmentName"

I am going to guess that the intention is something like this:

SELECT e.LastName as "Last Name", e.Gender as "Gender"
FROM Employee e
WHERE e.DepartmentId = (SELECT d.DepartmentId
                        FROM Department d
                        WHERE d.DepartmentName = 'Radiology'
                       );

That is, you want to connect the two tables on some sort of id . If you had DepartmentName in Employee , you would just write:

SELECT e.LastName as "Last Name", e.Gender as "Gender"
FROM Employee e
WHERE e..DepartmentName = 'Radiology';

No "join" would be needed.

The likelihood is that a department's name isn't stored in the employee table, but some "foreign key" to a row in the Department table is. Hence you would join those 2 tables

SELECT e.LastName as "Last Name", e.Gender as "Gender"
FROM Employee e
INNER JOIN Department d ON e.DepartmentId = d.id -- guessed this column name
WHERE d.DepartmentName = 'Radiology'

If confronted with a choice of join or IN(), use a join.

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