【发布时间】:2019-12-28 20:46:09
【问题描述】:
我有 2 个表(user、userContact),它们由 userID 列连接。
它们的结构有点像这样:
表用户:
[User_id]
[User_Name]
表UserContact:
[UserContact_id]
[User_id]
[UserContact_Type]
[UserContact_Description]
所以,对于同一个[User_id],可以有多个[UserContact_id]。
我的问题是:我想构建一个连接,它从表 User 中返回每个 [User_id],并在 [UserContact_Type] 等于 3 时包含 [UserContact_Description],但不要重复结果。
我尝试了什么:
SELECT DISTINCT
u.[User_id],
CASE
WHEN c.[UserContact_Type] = 3
THEN c.[UserContact_Description]
END AS UserContact
FROM
Table.user AS u
LEFT JOIN
Table.userContact AS c ON u.[User_id] = c.[User_id]
我得到这样的东西:
User_id UserContact
----------------------
6009 NULL
7010 NULL
7010 user7010@email.com
7012 NULL
8007 NULL
8007 user8007@email.com
它返回来自Table.user 的每个结果,但它会复制与不同[UserContact_Type] 相关联的每个[User_id] 的数据,而不是3。
我需要的是仅在 [UserContact_Type] = 3 时返回 [UserContact_Description]。
这是我在 StackOverflow 上的第一个问题。我希望我做得对,但请随时指出我在帖子中犯的任何错误。
非常感谢大家。
编辑:
正如您在下表中看到的,[User_id] 永远不会与同一个 [UserContact_Type] 相关
UserContact_id User_id UserContact_Type
40107 4747 1
40108 4747 5
40109 4747 3
41107 5748 6
41108 5748 3
41215 6009 1
41216 6009 6
42222 7010 3
42223 7010 6
43214 8007 3
我想要什么:当我找到与 [UserContact_Type] = 3 相关的 [User_id] 时,返回它的 [UserContact_Description]。如果[User_id] 与条件无关,则不返回任何内容并将[UserContact_Description] 列留空或为空。
我要避免的结果是这样的:
User_id UserContact
6009 NULL
7010 NULL
7010 user7010@email.com
[User_id] = 6009 没问题,但是 [User_id] = 7010 重复了,这是我的问题。我想要所有 [User_id]。但我只需要用 [UserContact_Type] 等于 3 来填充 UserContact 列。
希望现在更清楚。
【问题讨论】:
-
那么那些
NULL行不应该在那里? -
我认为您只需要添加另一个加入条件。像
LEFT JOIN Table.userContact as c ON u.[User_id] = c.[User_id] AND c.[UserContact_Type] = 3假设您知道表中始终只有一条该类型的记录。 -
另外,
TABLE和USER是 T-SQL 中的保留关键字,应避免用于对象名称和别名。如果这些是你的名字,那么你需要分隔标识。另外,您真的有一个名为[table]的架构吗?这就像有一个名为[column]的表:/ -
@Larnu 对 NULL 行没有问题。问题是重复的 [User_id] 行。不用担心,名称是虚构的,例如建议 =)
-
@ggon 您在 usercontact 表中的 (user_id, usercontact_type) 上有唯一的约束/索引吗?如果没有,那么您需要添加它或停止基于“如您所见......”的假设(并误导他人)。没有这个限制,你的假设最终会被发现并被证明是不正确的。
标签: sql sql-server join duplicates