MySQL将值拆分为行。创建功能时出错

时间:2015-02-10 08:21:08

标签: mysql sql

我想将列中的分隔字符串分隔成行。 例如

ID Value           ID Value  
---------          ----------  
1  a,b     into    1  a  
2  c,d             1  b  
                   2  c  
                   2  d  

我尝试过这些解决方案

1

create FUNCTION [dbo].[Split] (@sep VARCHAR(32), @s VARCHAR(MAX))
RETURNS TABLE
AS
    RETURN
    (
        SELECT r.value('.','VARCHAR(MAX)') as Item
        FROM (SELECT CONVERT(XML, N'<root><r>' + REPLACE(REPLACE(REPLACE(@s,'& ','&amp; '),'<','&lt;'), @sep, '</r><r>') + '</r></root>') as valxml) x
        CROSS APPLY x.valxml.nodes('//root/r') AS RECORDS(r)
    )

ERROR:
1.第一行[from [dbo]的语法错误。
2.有时错误在RETURNS处。

2

SELECT T1.reference, T1.name, T2.my_Splits AS subjects, T1.subtitile
FROM
 (
  SELECT *,
  CAST('<X>'+replace(T.subjects,',','</X><X>')+'</X>' as XML) as my_Xml 
  FROM archive T
 ) T1
 CROSS APPLY
 ( 
 SELECT my_Data.D.value('.','varchar(50)') as my_Splits
 FROM T1.my_Xml.nodes('X') as my_Data(D)
 ) T2

ERROR:
1.来自CAST行中AS XML的XML语法错误 2. CROSS APPLY的语法错误。它说JOIN丢失了。

3

SELECT A.[State],  
     Split.a.value('.', 'VARCHAR(100)') AS String  
 FROM  (SELECT [State],  
         CAST ('<M>' + REPLACE([City], ',', '</M><M>') + '</M>' AS XML) AS String  
     FROM  TableA) AS A CROSS APPLY String.nodes ('/M') AS Split(a);  

ERROR:
(来自Split.a.value(....

)的语法错误

我对SQL很陌生,无法完全理解互联网上提供的这些解决方案。它似乎适用于其他所有人,但我不知道为什么我一直得到这些语法错误。

3 个答案:

答案 0 :(得分:0)

将数据CSV存储在一个列中确实是一种糟糕的方法。考虑更改表格设计

SELECT id,
       Split.a.value('.', 'VARCHAR(100)') Value
FROM   (SELECT ID,
               Cast ('<M>' + Replace(Value, ',', '</M><M>') + '</M>' AS XML) AS Data
        FROM   archive) AS A
       CROSS APPLY Data.nodes ('/M') AS Split(a) 

SqlFiddle Demo

如果您需要某个功能,请使用此功能。

ALTER FUNCTION [dbo].[Split] (@sep VARCHAR(32),
                              @s   VARCHAR(MAX))
RETURNS TABLE
AS
    RETURN
      (SELECT Split.a.value('.', 'VARCHAR(100)') Value
       FROM   (SELECT Cast ('<M>' + Replace(@sep, @s, '</M><M>') + '</M>' AS XML) AS Data) AS A
              CROSS APPLY Data.nodes ('/M') AS Split(a)) 


SELECT id,
       cs.value
FROM   yourtable
       CROSS apply [dbo].[Split] (Value, ',') cs 

答案 1 :(得分:0)

仅当分隔符的每一侧都有一个字符时才有效:

select id, left(value,1) as value from db union select id, right(value,1) from db order by id, value asc;

答案 2 :(得分:0)

如果分割值的计数修复,您可以使用以下代码,示例三部分

SELECT * FROM (
  SELECT id, SUBSTRING_INDEX(value,',',1) AS value FROM tablename
  UNION
  SELECT id, SUBSTRING_INDEX(SUBSTRING_INDEX(value,',',2),',',-1) FROM tablename
  UNION
  SELECT id, SUBSTRING_INDEX(SUBSTRING_INDEX(value,',',3),',',-1) FROM tablename
) result
ORDER BY id

给出以下结果

id | value                   id | value
---+---------                ---+------
1  | 1,aa,xxx                1  | 1
2  | 2,yyy        into       1  | aa
3  | zzz,3,cc                1  | xxx
                             2  | 2
                             2  | yyy
                             3  | zzz
                             3  | 3
                             3  | cc