How to search multiple columns in MySQL?

MySQLMySQLi Database

Let us understand how to search multiple columns in MySQL −

Note: We assume we have created a database named ‘DBNAME’ and a table named ‘tableName’.

The ‘AND’ and ‘OR’ operators can be used, depending on what the user wants the search to return.

Let us see that with the help of an example −

Example

SELECT colName FROM tableName WHERE my_col LIKE %$param1% AND another_col LIKE
%$param2%;

In the above example, the ‘AND’ operator is used.

This means that both the clause has to match a record for the result to be returned.

Query

SELECT colName FROM tableName WHERE my_col LIKE %$param1% OR another_col LIKE
%$param2%;

In the above example, the ‘OR’ operator is used. This means that either of the clause have to match a record for the result to be returned.

raja
Published on 09-Mar-2021 13:18:46
Advertisements