sql server sub query with a comma separated resultset(带有逗号分隔结果集的 sql server 子查询)
问题描述
我需要返回一个表上的记录,我的结果集需要包含一个逗号分隔的列表.
我附上了一张 3 张桌子的图片.我需要执行一个选择,返回第一个表中的记录,并包含屏幕截图中第三个表中存在的最后一个 AwardFocusName.
所以我的结果集将返回一条记录并在其中包含 AwardFocusNames 列表(逗号分隔).
这是我过去用来做类似事情的一个技巧.使用 SUBSTRING 函数.
<前>SELECT n.nominationID, 子串((SELECT ',' + naf.awardFocusName来自提名奖Focus naf加入奖Focus afON naf.awardFocusID = af.awardFocusID其中 n.nominationID = naf.nominationIDFOR XML 路径('')), 2, 1000000)来自提名 n请注意,2 用于切断子选择添加到第一项的前导逗号,并且选择 1000000 作为一个大数字以表示字符串的所有其余部分".
I need to return records on a table and my result set needs to contain a comma separated list.
I have attached an image of the 3 tables. I need to do a select that returns the record in the first table and include the last of AwardFocusName that exist in the 3rd table in the screenshot.
So my result set would return one record and include the list of AwardFocusNames in it (comma separated).
Here's a trick I've used in the past to do similar things. Use SUBSTRING function.
SELECT n.nominationID , SUBSTRING(( SELECT ',' + naf.awardFocusName FROM NominationAwardFocus naf JOIN AwardFocus af ON naf.awardFocusID = af.awardFocusID WHERE n.nominationID = naf.nominationID FOR XML PATH('') ), 2, 1000000) FROM Nomination n
Note that the 2 is used to chop off the leading comma that the subselect adds to the first item, and 1000000 is chosen as a large number to mean "all of the rest of the string".
这篇关于带有逗号分隔结果集的 sql server 子查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:带有逗号分隔结果集的 sql server 子查询


- 导入具有可变标题的 Excel 文件 2021-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- SQL 临时表问题 2022-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 更改自动增量起始编号? 2021-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01