How can I split comma separated value in MySQL into rows?

前端 未结 1 541
后悔当初
后悔当初 2021-01-27 12:21

I have a function in MySQL that returns a list of integer values separated by a comma, thus forming a string.

e.g.

MyFunction(23);

Ret

相关标签:
1条回答
  • 2021-01-27 12:46

    You can use FIND_IN_SET function. For example:

    SELECT FIND_IN_SET('34', '34,45,87,23,1'); -- return true
    
    SELECT FIND_IN_SET('34', '34'); -- return true    
    
    SELECT FIND_IN_SET('34', ''); -- return false
    

    In your case, the SQL query will look like:

    SELECT id,
           myItemId,
           myItemValue,
           myDateTime
    FROM items
    WHERE id IN
        (SELECT MAX(id)
         FROM items
         WHERE myItemId=1
           AND FIND_IN_SET(myItemValue, MyFunction(123))
         GROUP BY myItemId);
    
    0 讨论(0)
提交回复
热议问题