首页 > 数据库技术 > 详细

数据库把1行查询为n行

时间:2017-10-25 14:45:52      阅读:244      评论:0      收藏:0      [点我收藏+]

一、效果展示

表中的数据:

技术分享

查询到的数据:

技术分享

二、SQL Server中实现

SELECT id,strAuthorId from cn_news where id<3;

with cte as
(select id,CHARINDEX(‘,‘,strAuthorId+‘,‘) as station,
 SUBSTRING(strAuthorId,1,CHARINDEX(‘,‘,strAuthorId+‘,‘)-1) as strAuthorIdSimple
 from cn_news
 union all
 select a.id,CHARINDEX(‘,‘,a.strAuthorId+‘,‘,b.station+1),
 SUBSTRING(a.strAuthorId,b.station+1,CHARINDEX(‘,‘,a.strAuthorId+‘,‘,b.station+1)-b.station-1)
 from cn_news as a
 join cte as b
 on a.id=b.id and CHARINDEX(‘,‘,a.strAuthorId+‘,‘,b.station+1)>0)
select * from cte where id<3;

  

数据库把1行查询为n行

原文:http://www.cnblogs.com/by-lhc/p/7728534.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!