Detailed explanation of common usage of MySQL query conditions

Detailed explanation of common usage of MySQL query conditions

This article uses examples to illustrate the common usage of MySQL query conditions. Share with you for your reference, the details are as follows:

condition

Use the where clause to filter the data in the table, and the rows with a true result will appear in the result set.

The syntax is as follows:

select * from table name where condition;

example:

select * from students where id=1;

Where supports multiple operators to process conditions

Comparison operator Logical operator Fuzzy query Range query Empty judgment

Comparison Operators

Equals: =
Greater than: >
Greater than or equal to: >=
Less than: <
Less than or equal to: <=
Not equal to: != or <>

Example 1: Query students whose ID is greater than 3

select * from students where id > 3;

Example 2: Query students whose ID is not greater than 4

select * from students where id <= 4;

Example 3: Query students whose name is not "Huang Rong"

select * from students where name != 'Huang Rong';

Example 4: Query students who have not been deleted

select * from students where is_delete=0;

Logical operators

and
or
not

Example 5: Query female classmates whose ID is greater than 3

select * from students where id > 3 and gender=0;

Example 6: Query students whose ID is less than 4 or has not been deleted

select * from students where id < 4 or is_delete=0;

Fuzzy query

like
% represents any number of characters
_ represents an arbitrary character

Example 7: Query students with the surname Huang

select * from students where name like '黄%';

Example 8: Search for students whose surname is Huang and whose first name is one character

select * from students where name like '黄_';

Example 9: Search for students with the surname Huang or Jing

select * from students where name like '黄%' or name like '%靖';

Range Query

in means in a non-contiguous range

Example 10: Query students whose ID is 1, 3, or 8

select * from students where id in(1,3,8);

between ... and ... means in a continuous range

Example 11: Query students whose IDs are 3 to 8

select * from students where id between 3 and 8;

Example 12: Query the boys whose IDs are 3 to 8

select * from students where (id between 3 and 8) and gender=1;

Empty judgment

Note: null is different from ''

Is null

Example 13: Query students who have not filled in their height

select * from students where height is null;

Is not null

Example 14: Query students who have filled in their height

select * from students where height is not null;

Example 15: Query boys who have filled in their height

select * from students where height is not null and gender=1;

Priority

The order of priority from high to low is: parentheses, not, comparison operators, logical operators

and is calculated before or. If both appear and you want to calculate or first, you need to use it in conjunction with ()

Readers who are interested in more MySQL-related content can check out the following topics on this site: "MySQL query skills", "MySQL common functions summary", "MySQL log operation skills", "MySQL transaction operation skills summary", "MySQL stored procedure skills" and "MySQL database lock related skills summary"

I hope this article will be helpful to everyone's MySQL database design.

You may also be interested in:
  • MySQL retrieves data based on the JSON field content as a query condition (including JSON arrays)
  • The difference and reasons between the MySQL query conditions not in and in
  • Detailed explanation of the problem of matching even when there is a space at the end of the string in the Mysql query condition
  • Will the index be used in the MySQL query condition?
  • Analysis of the difference between placing on and where in MySQL query conditions
  • MySQL explains how to optimize query conditions

<<:  js to realize a simple puzzle game

>>:  HTTPS Principles Explained

Recommend

The whole process record of Vue export Excel function

Table of contents 1. Front-end leading process: 2...

Solution to Django's inability to access static resources with uwsgi+nginx proxy

When deploying uwsgi+nginx proxy Django, access u...

An article teaches you how to implement a recipe system with React

Table of contents 1. Recipe Collection 1.1 Projec...

Detailed deployment of Alibaba Cloud Server (graphic tutorial)

I have recently learned web development front-end...

Detailed explanation of MySQL group sorting to find the top N

MySQL group sorting to find the top N Table Struc...

How to use jsonp in vue

Table of contents 1. Introduction 2. Installation...

Detailed explanation of the syntax and process of executing MySQL transactions

Abstract: MySQL provides a variety of storage eng...

Javascript operation mechanism Event Loop

Table of contents 1. Four concepts 1. JavaScript ...

Solve the matching problem in CSS

Problem Description As we all know, when writing ...

Install Docker for Windows on Windows 10 Home Edition

0. Background Hardware: Xiaomi Notebook Air 13/In...

Complete steps to configure IP address in Ubuntu 18.04 LTS

Preface The method of configuring IP addresses in...

MySQL uses binlog logs to implement data recovery

MySQL binlog is a very important log in MySQL log...

Install Centos7 using Hyper-v virtual machine

Table of contents introduce Prepare Download syst...

Example code for implementing verification code login in SMS API in Node

1. Node server setup + database connection The op...