SQL Server NULL 值与内连接

发布于 2024-11-03 10:17:38 字数 623 浏览 6 评论 0原文

我正在使用 C# 和 SQL Server。

看一下下面的 SQL:

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

它很简单并且运行良好。它可以很好地从 table1 表中检索数据,并正确地将 table1.empid 内部联接到 table2.nametable2.surname

现在,有时 table1.empid 为 null,当它为 null 时,此 SQL 只是忽略具有 null 值的“行”;根据标准,这是很正常的。

我在这里还需要获取具有空值的“行”,当 table1.empid 为空时,我需要为 table2.name 和 <代码>table2.姓氏。

我一直在玩 isnull() 但我所做的只是让它变得更糟。

有什么建议吗?

谢谢

I am using C# and SQL Server.

Take a look at the following 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.

Now, sometimes table1.empid is null and when it is, this SQL just ignores the "row" with the null value; 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.

I have been playing with isnull() but all I did is make it even worst.

Any suggestions?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

挽手叙旧 2024-11-10 10:17:38

您需要执行 LEFT JOIN:

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

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;
只是我以为 2024-11-10 10:17:38

尝试使用 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

Try using a 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
灯下孤影 2024-11-10 10:17:38

如果表 1 为空,而您仍然需要无法以此开头的记录。从 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

If table 1 is null and you still need the records that you cannot start with that. Start with table2 and join 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
七禾 2024-11-10 10:17:38
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

现在请注意,这还包括当 EmpID 不为空但仍然“无效”时的人员(如果他们在 table1 中有 EmpID,但在 table2 中找不到该 EmpID),所以如果您想避免这种情况,另一个选择是这样:

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
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:

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
庆幸我还是我 2024-11-10 10:17:38
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
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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文