简体   繁体   English

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

[英]SQL query - Fetch data from one column separated by comma and display it by row

I have a problem with fetching data separated by a comma. 我在获取以逗号分隔的数据时遇到问题。 I want the 我想要

Here is my problem 这是我的问题

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

I want to have a result like this. 我想要这样的结果。 Example when the user will choose 14 in TD the result should be like this: 例如,当用户在TD中选择14时,结果应如下所示:

ID | TD  |
1  | 14  |
2  | 13  |
3  | 12  |
4  | 11  |

And when the user will choose 32 in TD the result should be like this: 当用户将TD选择为32时,结果应如下所示:

ID | TD  |
1  | 32  |
2  | 89  |

when the user will select 23 the result should be like this: 当用户选择23时,结果应如下所示:

ID | TD  |
1  | 23  |
2  | 45  |
3  | 12  |

how to achieve this? 如何做到这一点?

You might try a stored procedure or function in your version of SQL. 您可以在您的SQL版本中尝试存储过程或函数。 This is MySql pseudo code and could be very buggy. 这是MySql伪代码,可能有很多错误。 Some SQL flavors do not support returning tables: 一些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

In order to make your database, you need to create a new table which has Id of Td and tdNos and have relationships with this. 为了创建数据库,您需要创建一个新表,该表具有Id为Td和tdNos并与此相关。 for example: 例如:

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     |

Which for the second table the combination of TdId and Priority are the primary key and it has a relation with table TdNos through TdId column 对于第二张表,TdId和Priority的组合是主键,并且通过TdId列与表TdNos有关系

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM