Mysql 我尝试使用并集、旋转和使用函数

Mysql 我尝试使用并集、旋转和使用函数,mysql,sql,Mysql,Sql,我试图寻找以下要求的解决方案 输入: ID Name Subject 1 james Maths 输出: ID Name Subject 1 j Maths 1 a Maths 1 m Maths 1 e Maths 1 s Maths 这里有人能帮我查询一下吗?你可以使用MySQL的函数来获取第一、第二、第三,。。。性格 子字符串[字符串],[位置],[长度] 范例 select ID, SUBSTRING(Name, 1

我试图寻找以下要求的解决方案

输入:

ID Name Subject 1 james Maths 输出:

ID Name Subject 1 j Maths 1 a Maths 1 m Maths 1 e Maths 1 s Maths 这里有人能帮我查询一下吗?

你可以使用MySQL的函数来获取第一、第二、第三,。。。性格

子字符串[字符串],[位置],[长度]

范例

select ID, SUBSTRING(Name, 1, 1) as Name, Subject from yourtable

在本例中,您应该能够编写自己的函数,以归档所需的输出。

这将通过递归查询完成,但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 <> '';