当前位置:网站首页>Sql Server STUFF与FOR XML PATH
Sql Server STUFF与FOR XML PATH
2022-07-28 16:32:00 【沧鹫小hai】

要求按type分组查询后,把相同type的,name列值合并,达到如下效果:

解析如下:
一、FOR xml path
FOR XML PATH 有的人可能知道有的人可能不知道,其实它就是将查询结果集以XML形式展现,有了它我们可以简化我们的查询语句实现一些以前可能需要借助函数或存储过程来完成的工作。那么以一个实例为主.
假设有张查询表如下

sql 一:
select type,name from tb_account where name like '%测试入职%' FOR xml path查询结果<row> <type>0</type> <name>测试入职登录</name> </row> <row> <type>1</type> <name>测试入职6</name> </row> <row> <type>0</type> <name>测试入职</name> </row>sql 二:
select type,name from tb_account where name like '%测试入职%' FOR xml path('')查询结果:
<type>0</type> <name>测试入职登录</name> <type>1</type> <name>测试入职6</name> <type>0</type> <name>测试入职</name>
sql 三:
select type,name from tb_account where name like '%测试入职%' FOR xml path('root')查询结果 :
<root> <type>0</type> <name>测试入职登录</name> </root> <root> <type>1</type> <name>测试入职6</name> </root> <root> <type>0</type> <name>测试入职</name> </root>sql 四:
select 10+type,','+name+',' from tb_account where name like '%测试入职%' FOR xml path('')查询结果
10,测试入职登录,11,测试入职6,10,测试入职,
二、stuff函数stuff(param1, startIndex, length, param2)
说明:将param1中自startIndex(SQL中都是从1开始,而非0)起,删除length个字符,然后用param2替换删掉的字符。备注
如果开始位置或长度值是负数,或者如果开始位置大于第一个字符串的长度,将返回空字符串。如果要删除的长度大于第一个字符串的长度,将删除到第一个字符串中的第一个字符。示例
以下示例在第一个字符串 abcdef 中删除从第 2 个位置(字符 b)开始的三个字符,然后在删除的起始位置插入第二个字符串,从而创建并返回一个字符串。SELECT STUFF('abcdef', 2, 3, 'ijklmn');
GO下面是结果集:
---------
aijklmnef
边栏推荐
- ionic中的$ionicPopup连续两个调用alert时需要注意的事项
- Hgu95av2. Online installation failed
- DOS command Daquan basic command + network common command
- Random talk on test platform - platform construction ideas (Part 1)
- ng-repeat在迭代最后一个元素时执行一个方法
- Master the key points of JVM interview
- 地图R语言
- 软件测试工程师如何突破职业发展瓶颈
- 从非儿童网站看基线安全到底有多重要
- Visual object class introduces Pascal VOC dataset
猜你喜欢

Deploy lamp platform -- compilation and installation of Linux, Apache, MySQL and PHP

地图R语言

Vscode uses eslint prettier to format code automatically
![【C语言进阶】——指针进阶[Ⅰ]](/img/62/d3410a61b931177fc02c1801489b5a.png)
【C语言进阶】——指针进阶[Ⅰ]

Punctual atomic serial port protocol

漫谈测试平台—平台建设思路(上)

Arya professional web automated test platform

Esp-mqtt-at instruction connects Alibaba cloud Internet of things platform

JVM performance tuning

Management of third-party technical services in product development
随机推荐
JS中为对象数组添加新对象结果导致数组中已存在的对象也修改了
小白必看的软件测试发展路线
Embedded development learning path
【C语言笔记分享】字符函数和字符串函数(建议收藏)
将input type='file' 类型的图片文件转成base64
医学公共数据库
Vscode uses eslint prettier to format code automatically
C # basic interview questions (with answers)
Management of third-party technical services in product development
Can‘t use an undefined value as an ARRAY reference at probe2symbol
软件测试干货
Hgu95av2. Online installation failed
谈谈“发布后问题”的度量
js将本地时间与服务器时间同步
【C语言进阶】——剖析入微数据在内存中的存储[上]
MySQL triggers
Master JVM interview topics and answers offer get soft (with learning roadmap)
软件测试零基础小白学习需要掌握哪些技能?
【C语言笔记分享】——动态内存管理malloc、free、calloc、realloc、柔性数组
Arya-专业web自动化测试平台