MySQL 中用于替代 `string_split(‘r,a,d,o’, ‘,’, ”)` 的方法是什么?

huangapple go评论59阅读模式
英文:

what is alternative for select string_split('r,a,d,o',',') in mysql

问题

将字符串转换为MySQL中的行。

select id from month_tab where name in (
string_split('jan,feb,mar,may')
);

在上述示例中,我们需要将**string_split('jan,feb,mar,may')** 返回为行。
最终输出应该如下所示
select id from month_tab where name in ('jan','feb','mar','may');
英文:

How to convert a string into rows in MySQL.

select id from month_tab where name in (
string_split('jan,feb,mar,may')
);

In the above example, we need to return string_split('jan,feb,mar,may') as rows.
the final output should be like
select id from month_tab where name in ('jan','feb','mar','may')
);

答案1

得分: 0

You can do it using JSON_TABLE

with cte as (
  select 'jan,feb,mar,may' as months
)
select month_tab 
from cte
CROSS JOIN JSON_TABLE(CONCAT('["', REPLACE(months, ',','","'), '"]'),
                      '$[*]' COLUMNS (month_tab TEXT PATH '$')) jsontable;
英文:

You can do it using JSON_TABLE

with cte as (
  select 'jan,feb,mar,may' as months
)
select month_tab 
from cte
CROSS JOIN JSON_TABLE(CONCAT('["', REPLACE(months, ',', '","'), '"]'),
                      '$[*]' COLUMNS (month_tab TEXT PATH '$')) jsontable;

huangapple
  • 本文由 发表于 2023年2月6日 17:18:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75359372.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定