Home > Database > Mysql Tutorial > body text

How to query with multiple conditions in mysql

下次还敢
Release: 2024-04-01 21:48:19
Original
2202 people have browsed it

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>
Copy after login

For example:

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

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>
Copy after login

For example:

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

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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!