source

mysql 각 데이터 행의 마지막 N자를 다음 행으로 이동합니다.

lovecheck 2023. 7. 26. 22:10
반응형

mysql 각 데이터 행의 마지막 N자를 다음 행으로 이동합니다.

각 데이터 라인의 마지막 3자를 다음 라인으로 이동하고 첫 번째 라인은 xxx로 채웁니다.

예:

enter image description here

이제 저는 1번 테이블을 가지고 있고, 2번 테이블을 원합니다, 감사합니다!

[업데이트]

mysql 버전은 5.7.22이며 지연 기능을 지원하지 않습니다.

MySql/MariaDB 버전이 창 기능을 지원하고 TableTwo를 이미 만든 경우 다음과 같이 새 행을 삽입할 수 있습니다.

insert into TableTwo(id, num)
select
  id,
  concat(
    coalesce(lag(right(num, 3)) over (order by id), 'xxx'), 
    coalesce(left(num, 2), '')
  ) num
from (
  select * from TableOne
  union all
  select max(id) + 1, null from TableOne
) t;

데모를 참조하십시오.

창 기능이 없으면 자체 조인으로 이 작업을 수행할 수 있습니다.

insert into TableTwo(id, num)
select
  t.id,
  concat(
    coalesce(right(t1.num, 3), 'xxx'), 
    coalesce(left(t.num, 2), '')
  ) num
from (
  select * from TableOne
  union all
  select max(id) + 1, null from TableOne
) t left join TableOne t1
on t1.id = t.id - 1;

데모를 참조하십시오.

결과:

> id | num  
> -: | :----
>  1 | xxxab
>  2 | cde01
>  3 | 23456
>  4 | 789

언급URL : https://stackoverflow.com/questions/63242645/mysql-move-the-last-n-characters-of-each-line-of-data-to-the-next-line

반응형