MySQL中是否有任何函数可以分解列的数据然后检索它? 就像列数据是P:12一样,那么数据可以在':'上展开然后读取吗?
答案 0 :(得分:2)
以下是关于mysql中SPLIT
问题的许多讨论:
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html
答案 1 :(得分:1)
您可以在MYSQL中编写一个Split功能,请检查此link
来自链接
CREATE FUNCTION SPLIT_STR(
x VARCHAR(255),
delim VARCHAR(12),
pos INT
)
RETURNS VARCHAR(255)
RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(x, delim, pos),
LENGTH(SUBSTRING_INDEX(x, delim, pos -1)) + 1),
delim, '');
用法
SELECT SPLIT_STR(string, delimiter, position)
实施例
SELECT SPLIT_STR('a|bb|ccc|dd', '|', 3) as third;
+-------+
| third |
+-------+
| ccc |
+-------+
答案 2 :(得分:0)
我使用while循环解析字符串,并将拆分项插入临时表。它很复杂,但我复制并粘贴代码,然后更改表名以便于阅读。这是一个将逗号分隔的用户ID列表解析为临时表的示例:
CREATE PROCEDURE spParse
(_UserList MEDIUMTEXT)
BEGIN
DECLARE _Pos INT;
DECLARE _Start INT;
DECLARE _Item INT;
DECLARE _Length INT;
CREATE TEMPORARY TABLE IF NOT EXISTS TempUserList
(
UserID INT
);
SET _Length = LENGTH(_UserList);
SET _Pos = 1;
SET _Start = 1;
divide_loop:LOOP
IF _Pos > _Length Then
LEAVE divide_loop;
End If;
IF SUBSTRING(_UserList,_Pos,1) = ',' Then
IF _Pos - _Start > 0 Then
IF IsNumeric(SUBSTRING(_UserList,_Start,_Pos-_Start)) Then
SET _Item = CONVERT(SUBSTRING(_UserList,_Start,_Pos-_Start),Signed);
INSERT INTO TempUserList (UserID)
VALUES (_Item);
End If;
End If;
SET _Start = _Pos + 1;
End If;
SET _Pos = _Pos + 1;
END LOOP divide_loop;
IF _Start <= _Length Then
If IsNumeric(SUBSTRING(_UserList,_Start,_Length - _Start + 1)) Then
SET _Item = CONVERT(SUBSTRING(_UserList,_Start,_Length - _Start + 1),Signed);
INSERT INTO TempUserList (UserID)
VALUES (_Item);
End If;
End If;
SELECT UserID FROM TempUserList;
DROP TABLE TempUserList;
END