如何显示空值的行?

时间:2016-09-30 02:21:01

标签: sql oracle

我试图找出如何使用" JobTitleID"显示空值的行。列。

SQL FILE:

DROP TABLE Employee;
DROP TABLE JobTitle;


-- create our table
CREATE TABLE JobTitle
(
JobTitleID Number(38) PRIMARY KEY,
JobTitle VARCHAR2(25)
)
;


CREATE TABLE Employee
(
EmployeeID Number(38) PRIMARY KEY,
EmployeeFName VARCHAR2(25),
EmployeeLName VARCHAR2(25),
ClientName VARCHAR2(25),
HoursWorked Number(5,2),
ChargeRate Number(5,2),
JobTitleID Number(38),
CONSTRAINT fkJobTitle FOREIGN KEY (JobTitleID) REFERENCES JobTitle(JobTitleID)
)
;



-- populate tables

INSERT ALL 
INTO JobTitle (JobTitleID, JobTitle) VALUES (1,'Project Manager')
INTO JobTitle (JobTitleID, JobTitle) VALUES (2,'Programmer')
INTO JobTitle (JobTitleID, JobTitle) VALUES (3,'Network Specialist')
INTO JobTitle (JobTitleID, JobTitle) VALUES (4,'Technical Support')
INTO JobTitle (JobTitleID, JobTitle) VALUES (5,'Cable Installer')
INTO JobTitle (JobTitleID, JobTitle) VALUES (6,'DBA')
INTO JobTitle (JobTitleID, JobTitle) VALUES (7,'Telecom Engineer')
SELECT * FROM dual;

INSERT ALL 
INTO Employee (EmployeeID, EmployeeFName,EmployeeLName,ClientName,HoursWorked,ChargeRate,JobTitleID) VALUES (1,'Bob','Smith','Acme Corp.',125.5,72.25,1)
INTO Employee (EmployeeID, EmployeeFName,EmployeeLName,ClientName,HoursWorked,ChargeRate,JobTitleID) VALUES(2,'Bob','Smith','Astro Electric',32,72.25,1)
INTO Employee (EmployeeID, EmployeeFName,EmployeeLName,ClientName,HoursWorked,ChargeRate,JobTitleID) VALUES(3,'Jane','Doe','Acme Corp.',160,25,2)
INTO Employee (EmployeeID, EmployeeFName,EmployeeLName,ClientName,HoursWorked,ChargeRate,JobTitleID) VALUES(4,'Jane','Doe','Astro Electric.',220,25,2)
INTO Employee (EmployeeID, EmployeeFName,EmployeeLName,ClientName,HoursWorked,ChargeRate,JobTitleID) VALUES (5,'Jane','Doe','Durham Tiles',12,25,2)
INTO Employee (EmployeeID, EmployeeFName,EmployeeLName,ClientName,HoursWorked,ChargeRate,JobTitleID) VALUES (6,'Henry','Jones','Lighting Unlimited',245,15,4)
INTO Employee (EmployeeID, EmployeeFName,EmployeeLName,ClientName,HoursWorked,ChargeRate,JobTitleID) VALUES(7,'Jane','Fonda','Whale Mart',275,72.25,1)
INTO Employee (EmployeeID, EmployeeFName,EmployeeLName,ClientName,HoursWorked,ChargeRate,JobTitleID) VALUES(8,'Bill','Murray','Whale Mart',145,20,5)
INTO Employee (EmployeeID, EmployeeFName,EmployeeLName,ClientName,HoursWorked,ChargeRate,JobTitleID) VALUES(9,'Bill','Murray','ABC Logistics',45,20,5)
INTO Employee (EmployeeID, EmployeeFName,EmployeeLName,ClientName,HoursWorked,ChargeRate,JobTitleID) VALUES (10,'John','Jameson','Whale Mart',160,20,5)
INTO Employee (EmployeeID, EmployeeFName,EmployeeLName,ClientName,HoursWorked,ChargeRate,JobTitleID) VALUES (11,'John','Jameson','ABC Logistics',130,20,5)
INTO Employee (EmployeeID, EmployeeFName,EmployeeLName,ClientName,HoursWorked,ChargeRate,JobTitleID) VALUES(12,'John','Jameson','ABM Systems',8,20,5)
INTO Employee (EmployeeID, EmployeeFName,EmployeeLName,ClientName,HoursWorked,ChargeRate,JobTitleID) VALUES(13,'Homer','Simpson','Flitter',345,25,2)
INTO Employee (EmployeeID, EmployeeFName,EmployeeLName,ClientName,HoursWorked,ChargeRate,JobTitleID) VALUES(14,'John','Carpenter','Speedy Messengers',25,15,4)
INTO Employee (EmployeeID, EmployeeFName,EmployeeLName,ClientName,HoursWorked,ChargeRate,JobTitleID) VALUES (15,'John','Carpenter','Flitter',123,15,4)
INTO Employee (EmployeeID, EmployeeFName,EmployeeLName,ClientName,HoursWorked,ChargeRate,JobTitleID) VALUES (16,'John','Carpenter','ABM Systems',67,15,4)
INTO Employee (EmployeeID, EmployeeFName,EmployeeLName,ClientName,HoursWorked,ChargeRate,JobTitleID) VALUES(17,'John','Carpenter','ABC Logistics',23,15,4)
INTO Employee (EmployeeID, EmployeeFName,EmployeeLName,ClientName,HoursWorked,ChargeRate,JobTitleID) VALUES(18,'Mary','Jane','ABM Systems',43,72.25,1)
INTO Employee (EmployeeID, EmployeeFName,EmployeeLName,ClientName,HoursWorked,ChargeRate,JobTitleID) VALUES(19,'Mary','Jane','Flitter',156,72.25,1)
INTO Employee (EmployeeID, EmployeeFName,EmployeeLName,ClientName,HoursWorked,ChargeRate,JobTitleID) VALUES (20,'Frank','Herbert','Acme Corp.',55,45,3)
INTO Employee (EmployeeID, EmployeeFName,EmployeeLName,ClientName,HoursWorked,ChargeRate,JobTitleID) VALUES (21,'Kathy','Smith','',0,45,3)
SELECT * FROM dual;



COMMIT;

到目前为止,我有这个:

COLUMN Employee FORMAT a25;
SELECT JOBTITLE.JOBTITLE AS "Job Title"
FROM EMPLOYEE
FULL JOIN JOBTITLE
ON EMPLOYEE.JOBTITLEID = JOBTITLE.JOBTITLEID
WHERE EMPLOYEE.JOBTITLEID = 0
ORDER BY "Job Title"; /*Ordering it by alphabetical order/*

现在我知道WHERE EMPLOYEE.JOBTITLEID = 0由于表JobTitle没有0作为有效JobTitleID而无法正常工作。

我正在寻求的输出是:

Job Title
-----------------------
DBA
Telecom Engineer

因为那些行没有任何"员工"。

我目前没有显示任何行。

3 个答案:

答案 0 :(得分:2)

您可以在作业标题表上使用left join

SELECT DISTINCT JOBTITLE.JOBTITLE AS "Job Title"
FROM JOBTITLE 
LEFT JOIN EMPLOYEE
ON EMPLOYEE.JOBTITLEID = JOBTITLE.JOBTITLEID
WHERE EMPLOYEE.JOBTITLEID IS NULL
ORDER BY "Job Title";

或使用not exists

select jobtitle 
from jobtitle j
where not exists (select 1 from employee where jobtitleid = j.jobtitleid)
order by 1

答案 1 :(得分:1)

你刚才改变

EMPLOYEE.JOBTITLEID = 0

EMPLOYEE.JOBTITLEID is null

因为在连接条件中连接表中的列的值是哪个 未找到将自动为null的示例

students
+-------------+--------------+---------
| id          | name         | section|
+-------------+--------------+---------
|           1 | Stud 1       |    1   | 
|           2 | Stud 2       |    3   |
+-------------+--------------+---------

sections

+-------------+--------------+----
| id          | name | teacher   |
+-------------+--------------+----
|           1 | A    | Teacher 1 |
|           2 | B    | Teacher 2 |
+-------------+--------------+----

Select students.id, students.name, sections.name from students
join sections on students.section = sections.id

RESULT

+-------------+--------------+---------
| id          | name         | section|
+-------------+--------------+---------
|           1 | Stud 1 |     |    A   | 
|           2 | Stud 2 |     |   null |
+-------------+--------------+---------

如您所见,连接中未找到的部分的结果为空

答案 2 :(得分:0)

你只需要检查表设计,如果你正在寻找没有值的字段是非数字的并且允许NULL然后你可以在where子句中使用“IS NULL”,否则如果你的数字你可以使用“= '0'“