Home  >  Article  >  Database  >  How to query with multiple conditions in mysql

How to query with multiple conditions in mysql

下次还敢
下次还敢Original
2024-04-01 21:48:192273browse

Use MySQL for multi-condition query

How to use MySQL for multi-condition query?

There are two main ways to perform multi-condition queries in MySQL:

1. Use the AND operator

AND operator will Multiple query conditions are combined together, and results will be returned only when all conditions are met. The syntax is as follows:

<code class="sql">SELECT column_list
FROM table_name
WHERE condition1 AND condition2 AND ...</code>

For example:

<code class="sql">SELECT *
FROM customers
WHERE age > 25 AND gender = 'male' AND city = 'New York';</code>

This query will return all customer records that are older than 25 years old, have a male gender, and live in New York City.

2. Use the OR operator

The OR operator combines multiple query conditions and returns results if any one of the conditions is met. The syntax is as follows:

<code class="sql">SELECT column_list
FROM table_name
WHERE condition1 OR condition2 OR ...</code>

For example:

<code class="sql">SELECT *
FROM products
WHERE price < 100 OR category = 'Electronics';</code>

This query will return all products whose price is less than 100 yuan or which belong to the "Electronics" category.

Note:

  • The AND operator has higher precedence than the OR operator.
  • You can use parentheses to control the execution order of operators.
  • You can also use other comparison operators, such as =, !=, >, <, >=, <=.

    The above is the detailed content of How to query with multiple conditions in mysql. 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
Previous article:what does mysql meanNext article:what does mysql mean