1

In tbl_1 I have:

id
1
2
3
4
5
6
7
8
9

in tbl_2:
id  value
1   1,2,3
2   5

Select * from tbl_1 where id in (Select value from tbl_2 where id = 2); --is OK

Select * from tbl_1 where id in (Select value from tbl_2 where id = 1);

--Need this resault: 3 rows:  1, 2 and 3
5
  • 2
    You shouldn't be storing comma delimited values to begin with. Do you have a chance to fix that broken design? Commented May 26, 2020 at 13:34
  • 1
    This is a bad data model. You probably would like to improve it. Otherwise your queries will become expensive (to write, to debug, and to run) over time. Commented May 26, 2020 at 13:34
  • @a_horse_with_no_name in value column store date as 1;2;3 ? Commented May 26, 2020 at 13:44
  • 2
    Don't store delimited values. Normalize your model and create a proper one-to-many relationship Commented May 26, 2020 at 13:46
  • @a_horse_with_no_name Ok, tnx Commented May 26, 2020 at 13:47

3 Answers 3

4

Fix your data model! You should not be storing numbers as strings. You should have properly declared foreign key relationships. Strings should not be used to store multiple values.

Sometimes, we are stuck with other people's really, really, really bad decisions. You can do what you want with `like:

Select t1.*
from tbl_1 t1
where exists (select 1
              from tbl_2 t2
              where t2.id = 1 and
                    ',' || t2.value || ',' like '%,' || t1.id ',%'
             );

However, your effort should be going into fixing the data, rather than trying to deal with it. The correct data would be a junction/association table with one row per id and value for table 2:

id  value
1   1
1   2
1   3
2   5
Sign up to request clarification or add additional context in comments.

Comments

2

Yet another option:

SQL> with
  2  -- sample data
  3  tbl_1 (id) as
  4    (select 1 from dual union all
  5     select 2 from dual union all
  6     select 3 from dual union all
  7     select 4 from dual union all
  8     select 5 from dual union all
  9     select 6 from dual union all
 10     select 7 from dual union all
 11     select 8 from dual union all
 12     select 9 from dual
 13    ),
 14  tbl_2 (id, value) as
 15    (select 1, '1,2,3' from dual union all
 16     select 2, '5,6,7' from dual
 17    )
 18  -- query which returns what you want
 19  select a.id
 20  from tbl_1 a join
 21    (select regexp_substr(b.value, '[^,]+', 1, column_value) id
 22     from tbl_2 b cross join
 23       table(cast(multiset(select level from dual
 24                           connect by level <= regexp_count(b.value, ',') + 1
 25                          ) as sys.odcinumberlist))
 26     where b.id = 1
 27    ) c on c.id = a.id;

        ID
----------
         1
         2
         3

SQL>

Comments

1

One option uses string functions:

select t1.*
from t1
inner join t2 on ',' || t2.value || ',' like '%,' || t1.id || ',%'
where t2.id = 1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.