mysql字符串转数组
时间: 2023-09-07 10:16:37 浏览: 330
Mysql通过存储过程分割字符串为数组
要将MySQL中的字符串转换为数组,你可以使用MySQL的内置函数`FIND_IN_SET()`和`SUBSTRING_INDEX()`来实现。下面是一个示例:
```sql
SET @str = 'apple,banana,orange';
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(@str, ',', numbers.n), ',', -1) AS item
FROM (
SELECT (a + b * 10 + 1) n
FROM (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) t
CROSS JOIN (SELECT 0 AS b UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) t2
ORDER BY n
) numbers
WHERE n <= (LENGTH(@str)
阅读全文