简体   繁体   English

SQL 服务器 NULL 值与内部连接

[英]SQL Server NULL value with inner join

I am using C# and SQL Server.我正在使用 C# 和 SQL 服务器。

Take a look at the following SQL:看看下面的SQL:

SELECT table1.id, table1.description, table2.name, table2.surname 
FROM table1 
    INNER JOIN table2 ON table1.EmpID = table2.EmpID

It is straight forward and works fine.这是直截了当的,工作正常。 It retrieves the data from table1 table just fine and inner joins table1.empid to table2.name and table2.surname correctly.它可以很好地从 table1 表中检索数据,并且内部将table1.empid正确连接到table2.nametable2.surname

Now, sometimes table1.empid is null and when it is, this SQL just ignores the "row" with the null value;现在,有时table1.empid是 null 而当它是时,这个 SQL 只是忽略具有 null 值的“行”; which is pretty normal basing on the criteria.根据标准,这很正常。

What I need here is to also get the "rows" with the null values and when table1.empid is null I need to set a custom value to table2.name and table2.surname .我在这里还需要使用 null 值获取“行”,当table1.empid为 null 时,我需要将自定义值设置为table2.nametable2.surname

I have been playing with isnull() but all I did is make it even worst.我一直在玩 isnull() 但我所做的只是让它变得更糟。

Any suggestions?有什么建议么?

Thanks谢谢

You need to do a LEFT JOIN:你需要做一个左联接:

SELECT table1.id, table1.description, table2.name, table2.surname FROM table1
LEFT JOIN table2 ON table1.EmpID = table2.EmpID;

Try using a UNION:尝试使用 UNION:

SELECT table1.id, table1.description, table2.name, table2.surname 
FROM table1 
INNER JOIN table2 ON table1.EmpID = table2.EmpID
UNION
SELECT table1.id, table1.description, 'Table 2 Null', 'Table 2 Null'
FROM table1
WHERE table1.empId is null
Select table1.id table1.description
    , Case When table1.EmpID Is Null Then 'Some Value' Else table2.name End As Table2Name
    , Case When table1.EmpID Is Null Then 'Some Value' Else table2.surname End As Table2Surname
From table1
    Left Join table2
        On table2.EmpID = table1.EmpID
Where table1.EmpID Is Null
        Or table2.EmpID Is Not Null

If table 1 is null and you still need the records that you cannot start with that.如果表 1 是 null 并且您仍然需要无法从该表开始的记录。 Start with table2 and join table1.从 table2 开始,加入 table1。

SELECT table1.id, table1.description, ISNULL(table1.empid, "some new value") AS name, table2.surname 
FROM table2 
    LEFT OUTER JOIN table1 ON table2.EmpID = table1.EmpID
SELECT table1.id
       ,table1.description
       ,COALESCE(table2.name, 'DEFAULT') AS name
       ,COALESCE(table2.surname, 'DEFAULT') AS surname
FROM table1 
LEFT JOIN table2
    ON table1.EmpID = table2.EmpID

Now note, that this will also include people when the EmpID is not null but nevertheless "invalid" if they have an EmpID in table1, but it isn't found in table2, so if that's something you want to avoid, another option is this:现在请注意,当 EmpID 不是 null 但如果他们在 table1 中有 EmpID 但在 table2 中没有找到它时仍然“无效”,这也将包括人,所以如果这是你想避免的事情,另一个选择是:

SELECT table1.id
       ,table1.description
       ,table2.name
       ,table2.surname
FROM table1 
INNER JOIN table2
    ON table1.EmpID = table2.EmpID

UNION ALL

SELECT table1.id
       ,table1.description
       ,'DEFAULT' AS name
       ,'DEFAULT' AS surname
FROM table1 
WHERE table1.EmpID IS NULL

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

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