Paulo Coghi
Paulo Coghi

Reputation: 14959

MySQL - How to use fields in 'LIKE' operator

I want to do a 'select' in MySQL using the operator 'LIKE'.

But I do not want to use text as a comparison factor. I want to compare text between two fields in same table, like this:

SELECT field1,field2 FROM table WHERE field2 LIKE %field1% ;

Is it possible?

Upvotes: 3

Views: 8147

Answers (2)

D'Arcy Rittich
D'Arcy Rittich

Reputation: 171401

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

Upvotes: 8

Pablo Santa Cruz
Pablo Santa Cruz

Reputation: 181280

Yes, it is. You can use:

SELECT field1,field2 FROM table WHERE field2 LIKE '%' + field1 '%' ;

Upvotes: 0

Related Questions