Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Substring inside string

Tags:

oracle

Suppose this is my table:

ID  STRING
1   'ABC'
2   'DAE'
3   'BYYYYYY'
4   'H'

I want to select all rows that have at least one of the characters in the STRING column somewhere in another row's STRING variable.

For example, 1 and 2 have an A in common and 1 ad 3 have a B in common, but 4 does not have any characters in common with any of the other rows. So my query should return only the first three lines.

I don't need to know with which line it matched.

Thanks!

like image 807
iomartin Avatar asked Dec 27 '22 16:12

iomartin


1 Answers

@A.B.Cade : Good solution but could be done without any distinct nor join.

SELECT * FROM test t1
WHERE EXISTS
(
  SELECT * FROM test t2
  WHERE t1.id<>t2.id AND
  regexp_like(t1.string, '['|| replace(t2.string, '.[]', '\.\[\]')||']')
)

The query won't compare the string with extra rows since it'll stop the comparison as soon as 1 match is found for the current row...

See fiddle.

like image 131
Francis P Avatar answered Jan 31 '23 18:01

Francis P