简体   繁体   English

TSQL左联接:不管结果如何显示结果

[英]TSQL Left Join: Show results regardless of match

I have 3 tables, dbo.employees, dbo.cars, dbo.hours. 我有3个表,dbo.employees,dbo.cars,dbo.hours。 I retrieve the three tables based on one another. 我检索基于彼此的三个表。 dbo.mployees always has a row to be shown. dbo.mployees总是有一行要显示。 dbo.cars sometimes does depending on employees, and dbo.hours sometimes does depending on cars. dbo.cars有时取决于员工,而dbo.hours有时取决于汽车。

SELECT          e.*, c.*, h.*
FROM            dbo.emploees e
LEFT JOIN       dbo.cars c ON e.id=c.employeeID
LEFT JOIN       dbo.hours h ON c.id=h.carID
WHERE           c.name='Honda City'
ORDER BY        e.id ASC;

I want to show all employees regardless. 我想向所有员工展示。 if there is a match on cars, show the car values, otherwise null values. 如果汽车匹配,则显示汽车值,否则显示空值。 if there is a match on huors, show them, otherwise null values. 如果相匹配,则显示它们,否则显示空值。

The statement I have works great if there are matches on all three tables, when there aren't it displays nothing. 如果在所有三个表上都有匹配项,那么我所拥有的语句就很好用,如果没有,则什么也不显示。 No employees at all. 根本没有员工。

There must be some other problems with your data cause your query is working perfectly fine. 您的数据肯定还有其他问题,因为您的查询工作正常。 Please check here demo 请在这里查看演示

SQL Demo SQL演示

If you want to filter records for left join then you need to add condition on join clause like below. 如果要过滤左连接的记录,则需要在join clause添加条件,如下所示。 If you add filter condition in where clause then it will filter whole result, so it will not returns other employees whose car name is null. 如果在where clause添加过滤条件,则它将过滤整个结果,因此不会返回其他汽车名称为null的员工。 Condition in where clause is ok for inner join , but not for left join . 条件where clause是OK的inner join ,而不是left join

SELECT          e.*, c.*, h.*
FROM            dbo.employees e
LEFT JOIN       dbo.cars c ON e.id=c.employeeID AND c.name='Honda City'
LEFT JOIN       dbo.hours h ON c.id=h.carID
ORDER BY        e.id ASC;
SELECT e.*, ch.*
FROM  dbo.employees e
LEFT JOIN       
(
Select c.*, h.*
From dbo.cars c
LEFT JOIN dbo.hours h ON c.id=h.carID
WHERE c.name='Honda City'
) ch 
ON e.id = ch.employeeID

ORDER BY e.id ASC;

As soon as you added the where clause all the employees with no car named Honda city got filtered out. 一旦添加了where子句,所有没有汽车,名为Honda city的员工都会被过滤掉。

If you want to show all employees who have either no car or a Honda City you will need to have the criteria in the WHERE clause: 如果要显示所有没有汽车本田市的员工,则需要在WHERE子句中具有以下条件:

SELECT          e.*, c.*, h.*
FROM            dbo.emploees e
LEFT JOIN       dbo.cars c ON e.id=c.employeeID
LEFT JOIN       dbo.hours h ON c.id=h.carID
WHERE           c.name='Honda City' OR c.ID IS NULL
ORDER BY        e.id ASC;

If you want to show all employees (regardless of car type), and only show the car details and hours when the car is a Honda City , you should have the criteria in the JOIN clause: 如果您想显示所有员工(与汽车类型无关),并且显示汽车是本田城的汽车的详细信息和工作时间,则应该在JOIN子句中具有条件:

SELECT          e.*, c.*, h.*
FROM            dbo.emploees e
LEFT JOIN       dbo.cars c ON e.id=c.employeeID AND c.name='Honda City'
LEFT JOIN       dbo.hours h ON c.id=h.carID
ORDER BY        e.id ASC;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM