1

I have a MySQL table called 'Employee'. It has seven columns, but only two column values are related to my question. The two column names are FullName and Name.

Here are some sample values of the two columns in the table for better understanding.

FullName                Name
----------
MichealPhilips      |   Philips
Louisfarak          |   louis
Waynebruce          |   kirten

I want to find the rows where FullName value contains the value of name. So in my example the answer should be MichealPhilips and Louisfarak but not Wayne bruce because FullName(Waynebruce) does not contain Name(Kirten).

I have tried a query some thing like this:

SELECT * FROM Employee WHERE FullName LIKE '%' || Name || '%';

But it seems like a wrong query. It is printing all the rows in the table and I don't know why.

Can someone please help me in this? Are there any other ways to write this query? Is my query wrong?

Sudhir kumar
  • 549
  • 2
  • 8
  • 31

1 Answers1

1

This should do it for you...

SELECT 
        a.FullName
FROM
        Employee a
WHERE
        a.FullName LIKE CONCAT('%', a.Name, '%')
Scott Barta
  • 79,344
  • 24
  • 180
  • 163
MYoussef
  • 46
  • 7
  • Thanks your query worked. I got the almost similar query having same problem here:- http://stackoverflow.com/questions/1398720/how-to-use-like-with-column-name. Any way thanks – Sudhir kumar Oct 25 '14 at 15:42
  • Your query is using ||, otherwise known as Logical OR. Basically you are querying all rows that are like % OR Name OR %. The wildcard will show you all rows. Therefore you need to concat the wildcard to the Name column. – MYoussef Oct 25 '14 at 15:54
  • First of all, that is Oracle DB documentation. MySQL Docs are located on the dev.mysql.com. See here : http://dev.mysql.com/doc/refman/5.5/en/logical-operators.html#operator_or – MYoussef Oct 25 '14 at 16:04
  • Sorry for the mistake!! – Sudhir kumar Oct 25 '14 at 16:08