Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use LIKE with column name

Tags:

sql

mysql

Normally LIKE statement is used to check the pattern like data.

example:

select * from table1 where name like 'ar%' 

My problem is to use one column of table with LIKE statement.

example:

select * from table1, table2 where table1.x is like table2.y% 

Query above results error . how to use one column data in like query?

like image 328
ArK Avatar asked Sep 09 '09 10:09

ArK


People also ask

How do I use like between two columns in SQL?

The SQL LIKE OperatorThe LIKE operator is used in a WHERE clause to search for a specified pattern in a column. There are two wildcards often used in conjunction with the LIKE operator: The percent sign (%) represents zero, one, or multiple characters.

Can we use like on date column in SQL?

Like operator will not work with DateTime.


2 Answers

You're close.

The LIKE operator works with strings (CHAR, NVARCHAR, etc). so you need to concattenate the '%' symbol to the string...


MS SQL Server:

SELECT * FROM table1,table2 WHERE table1.x LIKE table2.y + '%' 


Use of LIKE, however, is often slower than other operations. It's useful, powerful, flexible, but has performance considerations. I'll leave those for another topic though :)


EDIT:

I don't use MySQL, but this may work...

SELECT * FROM table1,table2 WHERE table1.x LIKE CONCAT(table2.y, '%') 
like image 136
MatBailie Avatar answered Oct 14 '22 08:10

MatBailie


SQL SERVER

 WHERE ColumnName LIKE '%'+ColumnName+'%' 
like image 32
RoRo Avatar answered Oct 14 '22 08:10

RoRo