Mysql SQL查询-从以逗号分隔的列中提取数据,并按行显示

Mysql SQL查询-从以逗号分隔的列中提取数据,并按行显示,mysql,sql,Mysql,Sql,我在获取用逗号分隔的数据时遇到问题。我想要这个 这是我的问题 Table ID | TDNO | PREVIOUS_TD | 1 | 14 | 13,12,11 | 2 | 23 | 45,12 | 3 | 32 | 89 | 4 | 55 | NEW | 我想要这样的结果。示例当用户在TD中选择14时,结果应如下所示: ID | TD | 1 | 14 | 2 | 13 | 3 | 12 | 4 |

我在获取用逗号分隔的数据时遇到问题。我想要这个

这是我的问题

Table
ID | TDNO | PREVIOUS_TD |
1  | 14   | 13,12,11    |
2  | 23   | 45,12       |
3  | 32   | 89          |
4  | 55   | NEW         |
我想要这样的结果。示例当用户在TD中选择14时,结果应如下所示:

ID | TD  |
1  | 14  |
2  | 13  |
3  | 12  |
4  | 11  |
ID | TD  |
1  | 32  |
2  | 89  |
ID | TD  |
1  | 23  |
2  | 45  |
3  | 12  |
当用户在TD中选择32时,结果应如下所示:

ID | TD  |
1  | 14  |
2  | 13  |
3  | 12  |
4  | 11  |
ID | TD  |
1  | 32  |
2  | 89  |
ID | TD  |
1  | 23  |
2  | 45  |
3  | 12  |
当用户选择23时,结果应如下所示:

ID | TD  |
1  | 14  |
2  | 13  |
3  | 12  |
4  | 11  |
ID | TD  |
1  | 32  |
2  | 89  |
ID | TD  |
1  | 23  |
2  | 45  |
3  | 12  |

如何实现这一点?

您可以在您的SQL版本中尝试一个存储过程或函数。这是MySql伪代码,可能有很多错误。某些SQL风格不支持返回表:

    create function returnCommaSepList (IN myId INT)
    begin
        --
        -- is mtId in the source table?
        SET @previousTD = (
            select PREVIOUS_TD 
            from TheTable
            where ID = myId
        )

        --
        -- if the result is NULL then id was not in the table, return
        if @previousTD IS NULL then return

        --
        -- create a temporary table
        create table #temp (
            id INT primary key autoincrement,
            td int
        )

        --
        -- add myId to the temp table
        insert into #temp (td) values(myId)

        --
        -- prepare to do the string handling.  Step through 
        -- @previousTD looking for commas
        SET @startPos = 0
        SET @commaPos = LOCATE(',', @previousTD, @startPos)

        --
        -- @commaPos will be NULL if the string is NULL
        if @commaPos IS NULL then return

        --
        -- @commaPos will be 0 if there are no commas in the string
        if @commaPos = 0 then 
            SET @previousTD = TRIM(@previousTD)

            --
            -- if @previousTD is empty then return
            if LENGTH(@previousTD) = 0 then return

            --
            -- @previousTD has something in it that is not a comma. 
            -- try to insert it and return
            insert into #temp (td) values(@previousTD)
            select * from #temp order bu id
            return
        endif

        --
        -- should have a @previousTD with at least 1 comma
        while @commaPos > 0
        begin
            SET @item = substring(@previousTD, @startPos, @commaPos)

            insert into #temp (td) values(TRIM(@item))

            SET @startPos = @commaPos + 1
            SET @commaPos = LOCATE(',', @previousTD, @startPos)
        end

        select * from #temp order bu id
    end

为了创建数据库,您需要创建一个Id为Td和tdNos的新表,并与之建立关系。例如:

Table TdNos
ID | TDNO | PREVIOUS_TD |
1  | 14   | 13,12,11    |
2  | 23   | 45,12       |
3  | 32   | 89          |
4  | 55   | NEW         |

Table TdNoHistory
TdID|Priority| PREVIOUS_TD |
1   |   1    |      13     |
1   |   2    |      12     |
1   |   3    |      11     |
2   |   1    |      45     |
2   |   2    |      12     |
3   |   1    |      89     |

对于第二个表,TdId和优先级的组合是主键,它通过TdId列与表TdNos有关系

您的数据库不是第一个标准格式()。对不起,我还是初学者,您能帮助我吗@E0K不要像下面的列表那样在一列中存储多个值:
13,12,11
,每个值都可以存储在单独的行中,可能存储在单独的子表中。你的数据库设计有误。请看下面的文章-这是这个网站上一个非常常见的问题。试试谷歌搜索。我不熟悉mysql,我可以为你编写MS SQL查询。但你想问什么?这个查询应该为您做什么?