-- mysql 行转换列咋整呢? --

luoyoumou 2011-05-08 12:10:00
有表:t(id, url)两个字段,
-- 数据格式如下:(其中urls中的每部分数据以逗号隔开
id urls
1 luoyoumou1,luoyoumou2,luoyoumou3,...,luoyoumouN
2 liming1,lili

-- 其url怎么转换成列的格式呢?
-- 型如:
1 luoyoumou1
1 luoyoumou2
...
1 luoyoumouN
2 liming1,
2 lili
...全文
585 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
gungod 2011-05-09
  • 打赏
  • 举报
回复
CSDN怎么了,怎么不让回复了?
wwwwb 2011-05-09
  • 打赏
  • 举报
回复
示例:
SELECT *,SUBSTRING_INDEX(
SUBSTRING_INDEX(a1.SUBJECT,',',b.id),',',-1)
FROM a a1 LEFT JOIN zz.lsb1 b
ON (LENGTH(a1.SUBJECT)-LENGTH(REPLACE(a1.SUBJECT,',','')))+1>=b.id;
LSB1:内容 1-20000,字段ID
rucypli 2011-05-08
  • 打赏
  • 举报
回复
luoyoumou 2011-05-08
  • 打赏
  • 举报
回复


-- 懒得到写函数、存储过程啦,用JOIN 整好啦!谢谢提醒!

create table t1(id int, names varchar(255));
create table t2(id int);

insert into t1(id,names) values(1,'luoyoumou1,luoyoumou2,luoyoumou3,luoyoumou4');
insert into t1(id,names) values(2,'lili1,lili2,lili3,lili');

insert into t2(id) values(1);
insert into t2(id) values(2);
insert into t2(id) values(3);
insert into t2(id) values(4);
insert into t2(id) values(5);

select t1.id, t2.id id2, reverse(substring_index(reverse(substring_index(concat(t1.names,','),',',t2.id)),',',1)) names
from t1, t2
where reverse(substring_index(reverse(substring_index(concat(t1.names,','),',',t2.id)),',',1))<>''
order by t1.id, t2.id;
luoyoumou 2011-05-08
  • 打赏
  • 举报
回复
-- 比如说:我现在有表 t, t2,如下:

root@localhost(test)10:34:29>select * from t;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
+------+
3 rows in set (0.00 sec)

root@localhost(test)10:34:32>select *from t2;
+------+---------------------------------------------+
| id | names |
+------+---------------------------------------------+
| 1 | luoyoumou1,luoyoumou2,luoyoumou3,luoyoumou4 |
| 1 | lili1,lili2,lili3,lili4 |
+------+---------------------------------------------+
2 rows in set (0.00 sec)


-- 请问:用JOIN语句的话,怎么写呢?
luoyoumou 2011-05-08
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 acmain_chm 的回复:]

可以通过存储过程 (速度比较快)

或者 通过 添加一个 辅助的序列表 T(ID INT),插入1,2,3,。。。。。10000 这样连接的记录。
然后通过 JOIN查询 和SUBSTRING_INDEX来生成结果。
[/Quote]

-- 如果用JOIN的话,怎么操作呢?
ACMAIN_CHM 2011-05-08
  • 打赏
  • 举报
回复
可以通过存储过程 (速度比较快)

或者 通过 添加一个 辅助的序列表 T(ID INT),插入1,2,3,。。。。。10000 这样连接的记录。
然后通过 JOIN查询 和SUBSTRING_INDEX来生成结果。

56,914

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧