Detailed explanation of using MySQL where

Detailed explanation of using MySQL where

1. Introduction

When we need to obtain a specific subset of database table data, we can use the where clause to specify search conditions for filtering. The where clause has a wide range of usage scenarios and is a key point in MySQL statements that you need to master. All the functions implemented by where can be implemented outside of MySQL, but filtering queries directly in MySQL is faster and saves network transmission overhead.

2. Main text

First, prepare a User table. The DDL and table data are as follows and can be copied and used directly.

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
 
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'Primary key',
  `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT 'User name',
  `age` int(11) NOT NULL COMMENT 'Age',
  `sex` smallint(6) NOT NULL COMMENT 'Gender',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 8 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
 
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES (1, '李子8', 18, 1);
INSERT INTO `user` VALUES (2, '张三', 22, 1);
INSERT INTO `user` VALUES (3, '李四', 38, 1);
INSERT INTO `user` VALUES (4, '王五', 25, 1);
INSERT INTO `user` VALUES (5, '六麻子', 13, 0);
INSERT INTO `user` VALUES (6, '田七', 37, 1);
INSERT INTO `user` VALUES (7, 'Thank you', 18, 1);
 
SET FOREIGN_KEY_CHECKS = 1;

The initial order of the data is as follows:

mysql> select * from user;
+----+--------+-----+-----+
| id | name | age | sex |
+----+--------+-----+-----+
| 1 | Plum 8 | 18 | 1 |
| 2 | Zhang San | 22 | 1 |
| 3 | Li Si | 38 | 1 |
| 4 | Wang Wu | 25 | 1 |
| 5 | Liu Mazi | 13 | 0 |
| 6 | Tianqi | 37 | 1 |
| 7 | Thank you | 18 | 1 |
+----+--------+-----+-----+
7 rows in set (0.00 sec)

2.1 Where Clause Position

The where clause comes after the from clause, for example:

mysql> select * from user where age=18;
+----+--------+-----+-----+
| id | name | age | sex |
+----+--------+-----+-----+
| 1 | Plum 8 | 18 | 1 |
| 7 | Thank you | 18 | 1 |
| 8 | Li Ziqi | 18 | 1 |
+----+--------+-----+-----+
3 rows in set (0.00 sec)

If you use order by, the where clause is placed before order by, for example:

mysql> select * from user where age = 18 order by name;
+----+--------+-----+-----+
| id | name | age | sex |
+----+--------+-----+-----+
| 1 | Plum 8 | 18 | 1 |
| 8 | Li Ziqi | 18 | 1 |
| 7 | Thank you | 18 | 1 |
+----+--------+-----+-----+
3 rows in set (0.00 sec)

2.2 Operators

The where clause supports 8 operators, which are as follows:

Operators Operator Description
= equal
<> Not equal to
!= Not equal to
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
BETWEEN AND The interval between two values, such as BETWEEN 1 AND 100

Next, use the operators in the table to perform where subqueries one by one.

2.2.1 Operator =

Operator = can be used to query data that matches exactly. Note that MySQL is not case sensitive by default.

need:
The query statement for data whose name is equal to Li Ziba is:

select * from user where name = '李子捌';

result:

+----+--------+-----+-----+
| id | name | age | sex |
+----+--------+-----+-----+
| 1 | Plum 8 | 18 | 1 |
+----+--------+-----+-----+
1 row in set (0.00 sec)

Operator = If there are multiple matching data, all data that meet the conditions of the where clause will be returned. If you need to specify the sorting method, you can use order by to sort the data.

2.2.2 Operators <> and !=

These two operators achieve the same effect, both matching unequal data.

need:
The query statement for data where name is not equal to Li Zi Ba is:

select * from user where name <> '李子捌';
select * from user where name != '李子捌';

result:

+----+--------+-----+-----+
| id | name | age | sex |
+----+--------+-----+-----+
| 2 | Zhang San | 22 | 1 |
| 3 | Li Si | 38 | 1 |
| 4 | Wang Wu | 25 | 1 |
| 5 | Liu Mazi | 13 | 0 |
| 6 | Tianqi | 37 | 1 |
| 7 | Thank you | 18 | 1 |
| 8 | Li Ziqi | 18 | 1 |
+----+--------+-----+-----+
7 rows in set (0.00 sec)

2.2.3 Operators <=, <, >=, >
These four operators are used to compare column data of numeric type, but if they are applied to text fields, MySQL can also perform the operations, but the returned results may not be the data you expect (in theory, no one will do this, but it will not report an error!)

need:
Query all users whose age is less than or equal to 20:

select * from user where age <= 20;

result:

+----+--------+-----+-----+

| id | name | age | sex |

+----+--------+-----+-----+

| 1 | Plum 8 | 18 | 1 |

| 5 | Liu Mazi | 13 | 0 |

| 7 | Thank you | 18 | 1 |

| 8 | Li Ziqi | 18 | 1 |

+----+--------+-----+-----+

4 rows in set (0.00 sec)

2.2.4 BETWEEN AND
BETWEEN AND is used to query the value between two numeric ranges. This range is two closed intervals, so it includes the starting value and the ending value. For example, BETWEEN 1 AND 100 includes data of 1 and 100.

need:
Query the user whose age is greater than or equal to 20 and less than or equal to 50:

select * from user where age between 20 and 50;

+----+------+-----+-----+

| id | name | age | sex |

+----+------+-----+-----+

| 2 | Zhang San | 22 | 1 |

| 3 | Li Si | 38 | 1 |

| 4 | Wang Wu | 25 | 1 |

| 6 | Tianqi | 37 | 1 |

+----+------+-----+-----+

4 rows in set (0.00 sec)

2.3 Null value

The null value means that it contains no data. It can be used to specify whether the column can contain null values ​​when creating a table. It should be noted that null is different from 0 of data value type and space of character type. The null value means no value.
Regarding the query of null value, MySQL provides a special where clause is null.

need:
Query the data statement with empty value for name:

select * from user where name is null;

result:

Empty set (0.00 sec)

Because there is no data in the user table with a null name value, no data is returned. If we need to query the data where the name column is not empty, how should we query it?

At this time we can use is not null

mysql> select * from user where name is not null;
+----+--------+-----+-----+
| id | name | age | sex |
+----+--------+-----+-----+
| 1 | Plum 8 | 18 | 1 |
| 2 | Zhang San | 22 | 1 |
| 3 | Li Si | 38 | 1 |
| 4 | Wang Wu | 25 | 1 |
| 5 | Liu Mazi | 13 | 0 |
| 6 | Tianqi | 37 | 1 |
| 7 | Thank you | 18 | 1 |
| 8 | Li Ziqi | 18 | 1 |
+----+--------+-----+-----+

This is the end of this article about the detailed use of MySQL where. For more relevant content on the use of MySQL where, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • MySQL Where conditional statement introduction and operator summary
  • MySQL left-join multi-table query where condition writing example
  • MySQL stored procedure parameter passing to implement where id in (1,2,3,...) example
  • MYSQL WHERE statement optimization
  • Detailed explanation of the usage of the WHERE clause in MySQL

<<:  Linux system calls for operating files

>>:  A brief discussion on tags in HTML

Recommend

JS achieves five-star praise case

This article shares the specific code of JS to ac...

The difference between div and table in speed, loading, web application, etc.

1: Differences in speed and loading methods The di...

Detailed explanation of the lock structure in MySQL

Mysql supports 3 types of lock structures Table-l...

SQL implementation of LeetCode (196. Delete duplicate mailboxes)

[LeetCode] 196.Delete Duplicate Emails Write a SQ...

Use of Linux gzip command

1. Command Introduction The gzip (GNU zip) comman...

MySQL 8.0.12 installation and configuration graphic tutorial

Recorded the download and installation tutorial o...

Ubuntu opens port 22

Scenario You need to use the xshell tool to conne...

Implementing search box function with search icon based on html css

Preface Let me share with you how to make a searc...

How to set process.env.NODE_ENV production environment mode

Before I start, let me emphasize that process.env...

A brief discussion on DDL and DML in MySQL

Table of contents Preface 1. DDL 1.1 Database Ope...

Details of MutationObServer monitoring DOM elements in JavaScript

1. Basic Use It can be instantiated through the M...

The main differences between MySQL 4.1/5.0/5.1/5.5/5.6

Some command differences between versions: show i...

JavaScript - Using slots in Vue: slot

Table of contents Using slots in Vue: slot Scoped...

Introduction to version management tool Rational ClearCase

Rational ClearCase is a software configuration ma...