Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to combine a column name into MySQL "LIKE" clause

I want to join two tables t1 and t2 such that column value in t2 is any valid string including null followed by a value in column of t table.

I want something like :

SELECT * FROM t_cities c JOIN temp_table t ON c.NAME LIKE "%t.token"but don't know the exact sytax. The above statement gives an error of course.

like image 760
Aneesh Avatar asked Oct 08 '13 11:10

Aneesh


People also ask

Can we use like operator on columns?

The 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.

How use multiple columns in SQL with like?

To select multiple columns from a table, simply separate the column names with commas! For example, this query selects two columns, name and birthdate , from the people table: SELECT name, birthdate FROM people; Sometimes, you may want to select all columns from a table.

How do I concatenate two columns in SQL query?

Query: SELECT *, CONCAT(FIRSTNAME, LASTNAME) AS FIRSTNAME FROM demo_table; Output: Here, we can see that FIRSTNAME and LASTNAME is concatenated but there is no space between them, If you want to add space between the FIRSTNAME and LASTNAME then add space(' ') in CONCAT() function.


1 Answers

Try this query, it should work.

SELECT * FROM t_cities as a  
JOIN temp_table as b  
ON a.NAME   LIKE concat("%",b.token);

Note- This query will not going to be fast as normal join and will take time.

like image 174
Ankit Sharma Avatar answered Sep 19 '22 13:09

Ankit Sharma