Home > Database > Mysql Tutorial > body text

How to Use LIKE in MySQL Join Queries?

DDD
Release: 2024-11-09 03:12:02
Original
221 people have browsed it

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 + '%'
Copy after login

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, '%')
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template