我尝试搜索以下要求的解决方案。
输入:
ID Name Subject 1 james Maths
输出:
ID Name Subject 1 j Maths 1 a Maths 1 m Maths 1 e Maths 1 s Maths
这里的任何人都可以帮我查询吗?
答案 0 :(得分:1)
您可以使用MySQL的SubString函数来获取第一个,第二个,第三个......字符。
SubString([String],[Position],[Length])
例如
select ID, SUBSTRING(Name, 1, 1) as Name, Subject from yourtable
通过这个例子,您应该能够编写自己的函数,以存档您想要的输出。
答案 1 :(得分:0)
这将通过递归查询来完成,但到目前为止,MySQL还没有递归查询。
如果您知道最大字母数(例如7),则可以
select id, name, subject
from
(
select id, substr(name, 1, 1) as name, subject from mytable
union all
select id, substr(name, 2, 1) as name, subject from mytable
union all
select id, substr(name, 3, 1) as name, subject from mytable
union all
select id, substr(name, 4, 1) as name, subject from mytable
union all
select id, substr(name, 5, 1) as name, subject from mytable
union all
select id, substr(name, 6, 1) as name, subject from mytable
union all
select id, substr(name, 7, 1) as name, subject from mytable
)
where name <> '';