I created two tables below, and FK_TypeID
is nullable in this case. I want to write a query returning me the join of two tables.
我在下面创建了两个表,在这种情况下FK_TypeID可以为空。我想写一个查询返回两个表的连接。
if FK_TypeID
is NULL for a certain row, the TypeName
is also NULL. I am not sure how to go about creating such join statement?
如果某个行的FK_TypeID为NULL,则TypeName也为NULL。我不知道如何创建这样的连接语句?
[Actions]
ActionsID
Date
Message
FK_TypeID //links to the table below
[Type]
TypeID
TypeName
My current statment looks like this and it simply skips NULL FK_TypeID
rows
我当前的语句看起来像这样,它只是跳过NULL FK_TypeID行
SELECT *
FROM Actions
INNER JOIN TypeName ON Actions.FK_TypeID = [Type].TypeID
Help would be greatly appreciated!
非常感谢帮助!
2 个解决方案
#1
20
You just need to use a LEFT JOIN
你只需要使用LEFT JOIN
SELECT Actions.Date, Actions.Message, Type.TypeName
FROM Actions
LEFT JOIN Type
ON Type.TypeID = Actions.FK_TypeID
If a match to Actions
is not found to tie it with Type
, then all columns in Type
will be NULL
.
如果未找到与Actions匹配的类型,则Type中的所有列都将为NULL。
Here is a good graphical visualization of the different types of joins in SQL
这是SQL中不同类型连接的良好图形可视化
#2
0
Use an OUTER JOIN
使用OUTER JOIN
SELECT Actions.Date, Actions.Message, Type.TypeName
FROM Actions LEFT OUTER JOIN
Type ON Type.TypeID = Actions.FK_TypeID
#1
20
You just need to use a LEFT JOIN
你只需要使用LEFT JOIN
SELECT Actions.Date, Actions.Message, Type.TypeName
FROM Actions
LEFT JOIN Type
ON Type.TypeID = Actions.FK_TypeID
If a match to Actions
is not found to tie it with Type
, then all columns in Type
will be NULL
.
如果未找到与Actions匹配的类型,则Type中的所有列都将为NULL。
Here is a good graphical visualization of the different types of joins in SQL
这是SQL中不同类型连接的良好图形可视化
#2
0
Use an OUTER JOIN
使用OUTER JOIN
SELECT Actions.Date, Actions.Message, Type.TypeName
FROM Actions LEFT OUTER JOIN
Type ON Type.TypeID = Actions.FK_TypeID