Home > OS >  Select records from a table where one column value lies in another column in the same table using SQ
Select records from a table where one column value lies in another column in the same table using SQ

Time:01-06

I want to select rows from a table where a column value lies in another column value of the same table. I am trying to execute code in Oracle SQL developer

I have a larger input dataset similar to below :

keys val set
1 H H, L , M
2 L P, Q , R
3 P P, S

I want to select only those records where my val column value lies in the set column of the same table. There can be multiple or single values in the set column The output would be like this :

keys val
1 H
3 P

How to achieve it using MySQL ? Please help.

**Note **: I have tried using below code. Its not working :

select keys, val from a where val in (set)

CodePudding user response:

you can do a simple comparison using concat :

SELECT `keys`, val
FROM dataset 
where `set` like concat('%',val,'%');

Try it here : https://dbfiddle.uk/HZ5spY-k

CodePudding user response:

I think it's need something like:

SELECT keys, val FROM a WHERE SET='val'

CodePudding user response:

I think you should try this

SELECT `keys`, `val` FROM `temp` WHERE FIND_IN_SET(`val`, `set`);
  • Related