只是想知道如何创建一个函数来计算mysql内部\ n \ n(连续2个)的数量并将其拆分为不同的别名
类似的东西:
FUNCTION
SELECT COUNT ('paragraphs of a text_field, "\n\n"') AS PRG
FROM database
WHERE field_name LIKE '%text%'
WHILE counter < PRG THEN SELECT SUBSTRING_INDEX (text_field, '\n\n', 1)
AS C+counter END WHILE
END FUNCTION;
输出应该是这样的
对于3段文字:
C1 |C2 |C3
Paragraph 1 |Paragraph 2 |Paragraph 3
对于5段文字:
C1 |C2 |C3 |C4 |C5
Paragraph 1 |Paragraph 2 |Paragraph 3 |Paragraph 4 |Paragraph 5
答案 0 :(得分:0)
MariaDB(以及MySQL)不支持拆分和转移 - 如果您在应用程序中进行计算,它肯定会更快。
纯MariaDB / MySQL解决方案将是一个存储过程,如
CREATE PROCEDURE xyz()
BEGIN
SELECT CHAR_LENGTH(field_name) - CHAR_LENGTH(REPLACE(field_name,"\n\n","")) INTO @a FROM mytbl WHERE fieldname='yourvalue';
SET @i=1;
SET @query="SELECT ";
REPEAT
SET @query= CONCAT(@query, ' REPLACE(substring_index(field_name, "\n\n", ',@i,'), SUBSTRING_INDEX(field_name,"\n\n", ',@i-1,'), "") AS C',@i,'');
IF @i < @a THEN
SET @query=CONCAT(@query, ",");
END IF;
SET @i=@i+1;
UNTIL @i > @a END REPEAT;
SET @query=CONCAT(@query, " FROM mytbl WHERE field_name='yourvalue'");
PREPARE my FROM @query;
EXECUTE my;
END