有表格:
create table test(a int,b varchar(1000));
insert into test(a,b)
values(1,'1*0*6|2*0*4|3*0*2|4*0*1'),(2,'3*0*6|7*0*4|3*8*2)
希望从此获得选择:
a b1 b2 b3
1, 1 , 0 , 6
1, 2, 0 , 4
1, 3, 0 , 2
1, 4, 0 , 1
2, 3, 0 , 6
2, 7, 0 , 4
2, 3, 8 , 2
怎么做?
答案 0 :(得分:0)
<强> POSTGRESQL 强>
select a, b[1], b[2], b[3]
from (
select a, regexp_split_to_array(b, '*') from test
) as dt(b)
<强> MYSQL 强>
SELECT
a,
SUBSTRING_INDEX(b,'*', 1) as b1,
SUBSTRING_INDEX(SUBSTRING_INDEX(b,'*', 2), '*', -1) as b2,
SUBSTRING_INDEX(SUBSTRING_INDEX(b,'*', 3), '*', -1) as b3
from test
答案 1 :(得分:0)
这不是一种非常有效的方法,但对于预期的输出,你可以尝试一下
select a,
SUBSTRING_INDEX(b, '*', 1) as b1,
SUBSTRING_INDEX(SUBSTRING_INDEX(b, '*', 2),'*',-1) as b2,
SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(b, '*', 3),'*',-1),'|',1) as b3
from test
答案 2 :(得分:0)
我使用此脚本将|
的字符串解析为行http://kedar.nitty-witty.com/blog/wp-content/uploads/2009/12/split_string.sql。
在我只有一个分隔符*
的行后,我将其拆分:
call split_string()
SELECT
id,
SUBSTRING_INDEX(splitted_column,'*', 1) as b1,
SUBSTRING_INDEX(SUBSTRING_INDEX(splitted_column,'*', 2), '*', -1) as b2,
SUBSTRING_INDEX(SUBSTRING_INDEX(splitted_column,'*', 3), '*', -1) as b3
from my_splits