Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find if one column contains another column

anyone know to how to create a query to find out if the data in one column contains (like function) of another column?

For example

ID||First_Name || Last_Name
------------------------
1 ||Matt       || Doe
------------------------
2 ||Smith      || John Doe
------------------------
3 ||John       || John Smith

find all rows where Last_name contains First_name. The answer is ID 3

thanks in advance

like image 483
msjsam Avatar asked Oct 27 '25 20:10

msjsam


2 Answers

Here's one way to do it:

Select *
  from TABLE
 where instr(first_name, last_name) >= 1;
like image 84
Marc Avatar answered Oct 30 '25 12:10

Marc


Try this:

select * from TABLE where last_name LIKE '%' + first_name + '%'
like image 41
har07 Avatar answered Oct 30 '25 12:10

har07



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!