左连接后 SQL Server MERGE 列?
我对 SQL Server 比较陌生,所以希望你能提供帮助。
基本上我有两个信息表。第一个看起来像:
- table1 [
ID, Name
] - table2 [
FKID, ID, Name
]
我想要执行如下语句:
SELECT table1.Name, table2.Name
FROM table1
LEFT JOIN table2 ON table2.FKID = table1.ID
这会导致类似的结果:
['Name 1a', 'Name 2a']
['Name 1b', NULL]
['Name 1c', NULL]
但我想要的是只选择一列,如果它不为空,则为 Name 1x
或 Name 2x
(左连接上没有关键匹配)。
我想典型的反应是使用我的编程语言来解决这个问题,但事实证明,在 Visual Web Developer 中使用 GridViews 和所有这些废话来解决这个问题是极其困难的,所以数据库解决方案将是非常有帮助的。
多谢!
I am relatively new to SQL Server so I hope you can help.
Basically I have two tables of information. The first looks like:
- table1 [
ID, Name
] - table2 [
FKID, ID, Name
]
I want to do a statement like:
SELECT table1.Name, table2.Name
FROM table1
LEFT JOIN table2 ON table2.FKID = table1.ID
This results in something like:
['Name 1a', 'Name 2a']
['Name 1b', NULL]
['Name 1c', NULL]
But what I want is to just select ONE column, that either being Name 1x
or Name 2x
if it wasn't null (there was no key match on the left join).
I imagine the typical response will be to solve this using my programming language, but this is proving to be extremely difficult in Visual Web Developer using GridViews and all this nonsense, so what would be very helpful would be a database solution.
Thanks a lot!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
COALESCE()
将返回第一个 NOT NULL 值。这是您要找的吗?COALESCE()
will return the first NOT NULL value. Is this what you're looking for?COALESCE()
将合并行。如果您想合并列,请尝试以下操作:COALESCE()
will merge rows. If you want to merge columns try something like: