I have a table i need to explode and split - generate from one row multiple rows based on app_permissions column value in MariaDB:
[email] | [app_permissions]
john@p.ai | {draft49:CAN_ACCESS_APP;CAN_VIEW_FINANCIAL_DATA}; {com.cc.worker:CAN_ACCESS_APP;CAN_VIEW_FINANCIAL_DATA}
This is the expected query result :
[email] | [app_permissions]
john@p.ai | draft49:CAN_ACCESS_APP
john@p.ai | draft49:CAN_VIEW_FINANCIAL_DATA
john@p.ai | com.cc.worker:CAN_ACCESS_APP
john@p.ai | com.cc.CAN_VIEW_FINANCIAL_DATA
I tried to cross join to the same table but it didn't produce this output
Also, there is no native "split_part" function for MariaDB, as far as i know and i'm trying to avoid creating procedures for this purpose.
DB : MariaDB 10.2
It is rather painful, but you can construct a list of numbers and then extract the nth substring between semicolons using substring_index()
.
You then need to remove other characters from the string that you don't seem to want:
select t.email,
replace(replace(replace(substring_index(substring_index(t.app_permissions, ';', n.n), ';', -1), '}', ''), '{', ''), ' ', '') as permission
from t join
(select 1 as n union all select 2 as n union all select 3 as n union all select 4 as n union all select 5) n
on t.app_permissions like concat('%', repeat(';%', n - 1));
I would recommend that you fix your data model, so you are not storing multiple values in a single string.
Here is a db<>fiddle.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.