How to Compare One Field to Another Using Like

Can I use 'like' to compare to a field in another table?

Remove the quotes from 'mask'. You don't want the STRING of mask, you want the VALUE of mask. I also changed the & to a + since my SQL Server (2012) doesn't allow the & for nvarchar data types.

SELECT FileName FROM tFiles 
WHERE FileName not in
(SELECT FileName from tFiles, tMasks where FileName like (mask + '%'));

I set up your described scenario on my database and it returned two rows:


Good luck!

MySQL - How to use fields in 'LIKE' operator

SELECT field1, field2 
FROM table
WHERE field2 LIKE CONCAT('%', field1, '%');

Compare two columns with 'LIKE' Operator in SQL / Laravel

Lets start by answering you question SQL-wise, anything in quotes is a literal to SQL, so you need to use column reference and add the wildcard symbols. You can do that like this:

SELECT * FROM Table1 WHERE email LIKE CONCAT('%', MOB, '%');

Now lets look at Laravel now, the 3rd argument to where expects a literal value not another column. You can overcome this via either whereRaw or DB::raw:

Table1::whereRaw("email LIKE CONCAT('%', MOB, '%')");


Table1::where('email', 'LIKE', DB::raw("CONCAT('%', MOB, '%')"));

Compare Columns Where One is Similar to Part of Another

Reverse the where, to something like this:

Fullname not like '%' + FirstName + '%' 

How to compare one field of a table with another field in a different table using like

You need to swap the search columns. See Pattern Matching

SELECT domain
FROM domains, keywords
WHERE domain like concat('%',keyword,'%');


SELECT '' LIKE CONCAT('%','shop','%');
| '' LIKE CONCAT('%','shop','%') |
| 1 |

Related Topics

Leave a reply