SELECT from table with Varying IN list in WHERE clause

前端 未结 2 648
终归单人心
终归单人心 2020-11-30 12:50

I am facing a issue in project I am working on, I can not give you actual code but I have created a executable sample code as below

Here temp and

2条回答
  •  误落风尘
    2020-11-30 13:29

    There is another way in which this might be accomplished, namely by using LIKE:

    SELECT ti.*
      FROM temp t, temp_id ti
     WHERE ',' || REPLACE(t.ids, ' ') || ',' LIKE '%,' || TO_CHAR(ti.data_id) || ',%'
    

    (I used REPLACE() above to get rid of the extraneous whitespace in the ID list - makes things a bit simpler.) Alternately, one could use REGEXP_LIKE():

    SELECT ti.*
      FROM temp t, temp_id ti
     WHERE REGEXP_LIKE(REPLACE(t.ids, ' '), '(^|,)' || TO_CHAR(ti.data_id) || '(,|$)')
    

    [The caret (^) and dollar-sign ($) characters are to match the start and end, respectively, of the string -- so the ID can either match something at the start of the string (ending with a comma or the end of the string) or something starting with a comma (again, ending with a comma or the end of the string).]

提交回复
热议问题