赞
踩
主要参考资料:http://blog.chinaunix.net/uid-411974-id-3990697.html
实现DEMO(Mysql)
create table testbzm(
id int,
note varchar(20)
);
insert into testbzm values(1,'xiaobai,xiaohuan,xiaohei');
insert into testbzm values(2,'111,222,333');
select * from testbzm;
+----+----------------------+
| id | note |
+----+----------------------+
| 1 | xiaobai,xiaohuan,xia |
| 2 | 111,222,333 |
+----+----------------------+
2 rows in set
-- 实现SQL
SELECT
id,
SUBSTRING_INDEX(
SUBSTRING_INDEX(note, ',', seq),
',' ,- 1
) own_by
FROM
( -- SEQ 生成1-100序列
SELECT
D1.seq + D2.seq * 10 seq
FROM
(select 0 seq union all
select 1 seq union all
select 2 seq union all
select 3 seq union all
select 4 seq union all
select 5 seq union all
select 6 seq union all
select 7 seq union all
select 8 seq union all
select 9 seq ) D1
CROSS JOIN (select 0 seq union all
select 1 seq union all
select 2 seq union all
select 3 seq union all
select 4 seq union all
select 5 seq union all
select 6 seq union all
select 7 seq union all
select 8 seq union all
select 9 seq ) D2
) sequence
CROSS JOIN testbzm
WHERE
seq BETWEEN 1
AND (
SELECT
1 + LENGTH(note) - LENGTH(REPLACE(note, ',', ''))
)
ORDER BY id
+----+----------+
| id | own_by |
+----+----------+
| 1 | xiaohuan |
| 1 | xia |
| 1 | xiaobai |
| 2 | 111 |
| 2 | 222 |
| 2 | 333 |
+----+----------+
6 rows in set
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。