sqlserver 将多行数据查询合并为一条数据

WBOY
Lepaskan: 2016-06-07 15:34:41
asal
2659 orang telah melayarinya

有这样一个需求:表T_FUN_TASK为任务表,有字段(TASKID,TASKNAME),表T_FUN_LOGBOOK为日志表,有字段(LOGID,TASKID,LOGDATE),一个任务可持续多天,每天会记录一条日志。在查询表T_FUN_TASK时,需将任务表中的 LOGDATE 查询出来作为一列 LOGDATES显示。

有这样一个需求:表T_FUN_TASK为任务表,有字段(TASKID,TASKNAME),表T_FUN_LOGBOOK为日志表,有字段(LOGID,TASKID,LOGDATE),一个任务可持续多天,每天会记录一条日志。在查询表T_FUN_TASK时,需将任务表中的 LOGDATE 查询出来作为一列 LOGDATES显示。

sqlserver 将多行数据查询合并为一条数据   

T_FUN_TASK

sqlserver 将多行数据查询合并为一条数据

T_FUN_LOGBOOK  

sqlserver 将多行数据查询合并为一条数据

查询结果

此结果查询方法可以用存储过程轻松实现,这里我要介绍的是sqlserver FOR XML PATH语句的应用,在SQL Server中利用 FOR XML PATH 语句能够把查询的数据生成XML数据

且合并为一条数据,看以下示例:

<span>SELECT</span> LOGDATE <span>FROM</span> T_FUN_LOGBOOK <span>WHERE</span> TASKID<span>=</span><span>231</span> <span>FOR</span> XML PATH
Salin selepas log masuk

结果为:


2014-01-06T00:00:00


2014-01-07T00:00:00

首先,将日期格式转化为需要的格式:

<span>SELECT</span> <span>CONVERT</span>(<span>VARCHAR</span>(<span>100</span>), LOGDATE, <span>111</span>) <span>FROM</span> T_FUN_LOGBOOK <span>WHERE</span> TASKID<span>=</span><span>231</span> <span>FOR</span> XML PATH
Salin selepas log masuk

结果变为:

2014/01/06
2014/01/07

<span>SELECT</span> <span>CONVERT</span>(<span>VARCHAR</span>(<span>100</span>), LOGDATE, <span>111</span>) <span>FROM</span> T_FUN_LOGBOOK <span>WHERE</span> TASKID<span>=</span><span>231</span> <span>FOR</span> XML PATH('')
Salin selepas log masuk

现在两条结果之间很难区分,需要用下划线将其分隔开来,方法是在CONVERT函数前面加上一个下划线:

<span>SELECT</span> <span>'</span><span>_</span><span>'</span><span>+</span><span>CONVERT</span>(<span>VARCHAR</span>(<span>100</span>), LOGDATE, <span>111</span>) <span>FROM</span> T_FUN_LOGBOOK <span>WHERE</span> TASKID<span>=</span><span>231</span> <span>FOR</span> XML PATH(<span>''</span>)
Salin selepas log masuk

结果:_2014/01/06_2014/01/07。

在分析了FOR XML PATH语句之后,就来将这个查询结果添加到对T_FUN_TASk的查询结果中去。我的思路是先构建一个子查询,然后查询TASK表时LEFT JOIN这个子查询

<span>SELECT</span> T1.TASKID,<span>'</span><span>TASKNAME</span><span>'</span> <span>AS</span> TASKNAME,T2.LOGDATES <span>FROM</span><span> T_FUN_TASK T1

</span><span>LEFT</span> <span>JOIN</span> (<span>SELECT</span><span> TASKID, 
LOGDATES</span><span>=</span>(<span>SELECT</span> <span>'</span><span>_</span><span>'</span><span>+</span><span>CONVERT</span>(<span>VARCHAR</span>(<span>100</span>), LOGDATE, <span>111</span>) <span>FROM</span> T_FUN_LOGBOOK <span>WHERE</span> TASKID<span>=</span>T1.TASKID <span>FOR</span> XML PATH(<span>''</span>)) <span>FROM</span><span> T_FUN_LOGBOOK T1 
</span><span>GROUP</span> <span>BY</span> TASKID) T2 <span>ON</span> T2.TASKID<span>=</span><span>T1.TASKID

</span><span>ORDER</span> <span>BY</span> T1.TASKID <span>ASC</span>
Salin selepas log masuk

运行以上SQL后得到的结果为:

sqlserver 将多行数据查询合并为一条数据

发现LOGDATES值的第一个下划线应该去掉,于是修改SQL,应用 STUFF函数去掉第一个下划线:

<span>SELECT</span> T1.TASKID,<span>'</span><span>TASKNAME</span><span>'</span> <span>AS</span> TASKNAME,T2.LOGDATES <span>FROM</span><span> T_FUN_TASK T1

</span><span>LEFT</span> <span>JOIN</span> (<span>SELECT</span><span> TASKID, 
LOGDATES</span><span>=</span><span>STUFF</span>((<span>SELECT</span> <span>'</span><span>_</span><span>'</span><span>+</span><span>CONVERT</span>(<span>VARCHAR</span>(<span>100</span>), LOGDATE, <span>111</span>) <span>FROM</span> T_FUN_LOGBOOK <span>WHERE</span> TASKID<span>=</span>T1.TASKID <span>FOR</span> XML PATH(<span>''</span>)),<span>1</span>,<span>1</span>,<span>''</span>) <span>FROM</span><span> T_FUN_LOGBOOK T1 
</span><span>GROUP</span> <span>BY</span><span> TASKID) T2
</span><span>ON</span> T2.TASKID<span>=</span><span>T1.TASKID

</span><span>ORDER</span> <span>BY</span> T1.TASKID <span>ASC</span>
Salin selepas log masuk

将结果转化为JSON 返回给前端之后,JS按照下划线分隔这个字段的值,即可以得到一个任务下面日志的填写情况。

 

 

 

 

sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan