Home Database Mysql Tutorial mysql advanced (5) multi-condition query with OR in data table

mysql advanced (5) multi-condition query with OR in data table

Feb 09, 2017 pm 03:18 PM
data sheet

Multi-condition query with OR in MySQL data table

The OR keyword can be combined with multiple conditions for query. When using the OR keyword:

Conditions

1) As long as one of these query conditions is met, such records will be queried.

2) If any of these query conditions are not met, such records will be excluded.

Grammar format

The basic syntax format of the OR keyword is as follows:

Conditional expression 1 OR conditional expression 2 [...OR conditional expression n]

OR can connect two conditional expressions, and multiple OR keywords can be used to connect more conditional expressions.

Example

Example 1

Use the OR keyword to query the records in the employee table whose d_id is 1001, or whose sex is 'male'. The code of the SELECT statement is as follows:

SELECT * FROM employee WHERE d_id=1001 OR sex LIKE '男';
Copy after login

The record with d_id 1001 can be queried. At the same time, the record with sex as 'male' has also been queried.

This shows that when using the OR keyword, as long as it meets any one of multiple conditions, it can be queried.

Example 2

Use the OR keyword to query the records in the employee table. The query condition is that the value of num is in the (1,2,3) set, or the range of age is 24 to 26, or the value of homeaddress contains "Xiamen City". The code of the SELECT statement is as follows:

SELECT * FROM employee WHERE
         num IN(1,2,3) OR age BETWEEN 24 AND 26
         OR homeaddress LIK '%厦门市%';
Copy after login

The results show that as long as any one of these three conditional expressions is met, such records will be queried.

OR can be used together with AND. When the two are used together, AND operates before OR.

Example 3

Use OR and AND keywords to query records in the employee table. The code of the SELECT statement is as follows:

SELECT * FROM employee WHERE num IN(1,3,4) AND age=25 OR sex='女';
Copy after login

The results show that as long as the records matching num IN(1,3,4) AND age=25 or the records matching sex='female' are displayed.

If you change the order of the conditions, the SELECT statement becomes the following situation:

SELECT * FROM employee WHERE sex='女' OR num IN(1,3,4) AND age=25;
Copy after login

The result shows that the execution result of the previous SELECT statement is the same.

This shows that the conditions before and after the AND keyword are combined first, and then combined with the conditions of the OR keyword. In other words, AND is calculated before OR.

Tips

AND and OR keywords can connect conditional expressions. Operators such as "=" and ">" can be used in these conditional expressions, as well as keywords such as IN, BETWEEN AND and LIKE. Moreover, the LIKE keyword can use "%" and "_" when matching strings. Wait for wildcards.

The above is the content of the multi-condition query with OR in the mysql advanced (5) data table. For more related content, please pay attention to the PHP Chinese website (www.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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1272
29
C# Tutorial
1251
24
Data table compression technology in MySQL Data table compression technology in MySQL Jun 16, 2023 am 08:16 AM

MySQL is a common relational database that is a core component of many websites and applications. As the amount of data becomes larger and larger, how to optimize the performance of MySQL becomes particularly important. One of the key areas is the compression of data tables. In this article we will introduce the data table compression technology in MySQL. Compressed tables and non-compressed tables There are two types of data tables in MySQL: compressed tables and non-compressed tables. Uncompressed tables are MySQL's default table type, which use fixed-length row format to store data. This means data

Data table DDL operation technology in MySQL Data table DDL operation technology in MySQL Jun 15, 2023 pm 07:55 PM

MySQL is a very popular open source relational database management system that supports complete DDL (data definition language) operations. DDL is a language used to define and manage various data objects in the database, including data tables, views, indexes, etc. It is very important for database administrators and developers to be proficient in DDL operation technology of data tables in MySQL. This article will introduce in detail the technology and methods of DDL operation of data tables in MySQL, and provide practical operation examples. 1. Create a data table. Creating a data table is in DDL.

Data table reloading techniques in MySQL Data table reloading techniques in MySQL Jun 15, 2023 pm 11:28 PM

MySQL is an open source relational database management system. Its basic functions are excellent in database design, data storage and management. In MySQL, the data table is the most basic unit of data storage. In practical applications, data table reloading is a very common operating technique, which can help us improve the operating efficiency of the database and improve the stability of the system. This article will introduce this operation technique in detail from the concepts, principles and practical applications of data table overloading in MySQL. 1. What is data table overloading? Data table overloading is

mysql modify data table name mysql modify data table name Jun 20, 2023 pm 05:52 PM

MySQL modifies the data table: 1. First check all tables in the database, the code is: "SHOW TABLES;"; 2. Modify the table name, the code is: "ALTER TABLE old table name RENAME [TO] new table name;". 3. Check whether the table name is modified successfully. The code is: "SHOW TABLES;"

How to calculate the average value of numeric columns in a data table using MySQL's AVG function How to calculate the average value of numeric columns in a data table using MySQL's AVG function Jul 24, 2023 pm 09:52 PM

Introduction to the method of using MySQL's AVG function to calculate the average value of numeric columns in a data table: MySQL is an open source relational database management system with a wealth of built-in functions to process and calculate data. Among them, the AVG function is a function used to calculate the average of a numeric column. This article will introduce how to use the AVG function to calculate the average value of numeric columns in a MySQL data table, and provide relevant code examples. 1. Create a sample data table First, we need to create a sample data table for demonstration. Suppose we have a file called

How to use thinkorm to implement related queries between data tables How to use thinkorm to implement related queries between data tables Aug 01, 2023 am 08:25 AM

How to use thinkorm to implement related queries between data tables Introduction: During database development, we often encounter situations where we need to perform related queries between multiple data tables. Using thinkorm, an excellent database ORM framework, you can easily implement associated queries of data tables and improve development efficiency. This article will introduce how to use thinkorm to implement related queries between data tables, and provide code examples to help readers better understand. 1. Basic concepts Before performing related queries, you first need to understand th

How to implement MySQL underlying optimization: horizontal and vertical splitting strategies for data tables How to implement MySQL underlying optimization: horizontal and vertical splitting strategies for data tables Nov 08, 2023 pm 06:57 PM

How to realize the underlying optimization of MySQL: horizontal and vertical partitioning strategies of data tables, which require specific code examples. Introduction: In large-scale application scenarios, MySQL databases often face the pressure of storing and querying massive data. In order to solve this problem, MySQL provides data table partitioning strategies, including horizontal partitioning (HorizontalPartitioning) and vertical partitioning (VerticalPartitioning). This article will introduce how to implement MySQL underlying optimization, focusing on

How to use the MAX function in MySQL to find the largest value in the data table How to use the MAX function in MySQL to find the largest value in the data table Jul 25, 2023 pm 09:49 PM

How to use the MAX function in MySQL to find the maximum value in the data table Introduction: In MySQL, we often need to perform various queries and analysis on the data table, including finding the maximum value in the data table. The maximum value in a data table can be easily found using the MAX function and is very useful when further processing the data. This article will introduce how to use the MAX function to find the largest value in the data table, and give corresponding code examples. 1. Introduction to the MAX function The MAX function is an aggregate function in MySQL. Use

See all articles