Home  >  Article  >  Database  >  How to Use LIKE in MySQL Join Queries?

How to Use LIKE in MySQL Join Queries?

DDD
DDDOriginal
2024-11-09 03:12:02222browse

How to Use LIKE in MySQL Join Queries?

Using LIKE in MySQL Join Queries

In MySQL, join queries using the LIKE operator can prove challenging. One common attempt is the following:

SELECT *
FROM Table1
INNER JOIN Table2 ON Table1.col LIKE '%' + Table2.col + '%'

However, this approach often fails. The solution lies in properly concatenating strings in MySQL, as it differs from other databases.

To resolve the issue, use the following syntax:

SELECT *
FROM Table1
INNER JOIN Table2 ON Table1.col LIKE CONCAT('%', Table2.col, '%')

MySQL handles string concatenation differently, requiring the use of the CONCAT() function to explicitly concatenate the strings. Note that the || operator for string concatenation does not work in MySQL as it is reserved for the logical OR operation.

The above is the detailed content of How to Use LIKE in MySQL Join Queries?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn