当前位置:网站首页>[getting started with MySQL] fourth, explore operators in MySQL with Kiko
[getting started with MySQL] fourth, explore operators in MySQL with Kiko
2022-07-06 17:37:00 【kikokingzz】
Write it at the front
Have to say , It's really, really hot ! Go out and deliver something this afternoon , As a result, I was hot and sweaty , The sun shines on me , It feels like looking at me with thousands of mirrors , Gather at one point , It made me a little confused and trance . This heat wave and irritability grow exponentially in the quiet air . At this time , There is only one idea in my mind , That's it , Hurry to be more polite ! We can't wait any longer !
So today will bring two more , The content of this sentence is mainly to introduce MySQL Some operators commonly used in , These operators will be applied to , So you can collect it well , Savor it carefully !
Systematic explanation in previous periods :
Catalog
Knowledge point 6:MySQL Operator in
5.1 Numeric and character comparison operators
5.2 Logical comparison operator
5.4 Match empty 、 Non air transport operators
Knowledge point 6:MySQL Operator in
Before formally explaining operators , Let's create a new one called hero Table for , In this table, we insert some game data , We used some statements and data types when creating tables , for example int、tinyint、enum And so on are mentioned in the previous chapter , If you have forgotten , Hurry to review !
【MySQL introduction 】 Third words · MySQL Common data types in https://blog.csdn.net/qq_54151955/article/details/125401775?spm=1001.2014.3001.5501
# Create a hero Data sheet
USE kiko_1;
CREATE TABLE hero(
id int,
name char(20),
attack int unsigned,
defense tinyint unsigned,
sex enum(" male "," Woman "),
country varchar(20)
)CHARACTER SET utf8;
INSERT INTO hero VALUES
(1,' Zhugeliang ',120,20,' male ',' Shu Kingdom '),
(2,' Sima yi ',119,25,' male ',' State of Wei '),
(3,' Guan yu ',188,60,' male ',' Shu Kingdom '),
(4,' zhaoyun ',200,66,' male ',' State of Wei '),
(5,' king of Wu in the Three Kingdoms Era ',110,20,' male ',' Wu kingdom '),
(6,' The sable cicada ',666,10,' Woman ',' State of Wei '),
(7,null,1000,99,' male ',' Shu Kingdom '),
(8,'',1005,88,' Woman ',' Shu Kingdom '),
(9,' Liu bei ',146,90,' male ',' Shu Kingdom ');
After creating the above table , We're about to start learning something formally MySQL Common operators in !
5.1 Numeric and character comparison operators
The reason why these two are put together is that they first look very similar , however Numerical comparison Size comparison is possible , and Character comparison You can only compare whether the characters are the same , These two operators are usually applied to WHERE In the condition of , Its specific characteristics are as follows :
Numerical comparison : = != > >= < <= Character comparison : = !=
In order to distinguish the two more clearly , At the same time, consolidate the practice , We can practice the following two questions :
1、 Find attack power higher than 150 The name and attack value of the hero .( Numerical comparison )
In this question WHERE Conditions Here we use self Numeric comparison operators >, take attack Attack value and 150 Compare the numbers .
SELECT name,attack FROM hero WHERE attack>150;
2、 Set Zhao Yun's attack power to 360, Defense is set to 200.( Character comparison )
This involves modifying the records in the table , Therefore use UPDATE command , stay WHERE The conditions should limit the hero , At this point, the character comparison operator is used .
UPDATE hero SET attack=360,defense=200 WHERE name=' zhaoyun ';
I'm the divider
5.2 Logical comparison operator
Logical comparison operators are usually used when making multiple conditional judgments , Play a role of connection or combination restriction , Usually, there are two kinds :
- AND: Two or more conditions are required to be true at the same time .
- OR: Any condition is required to be true .
For the sake of understanding , Similarly, we can further understand through two examples .
1、 Find attack power higher than 200 The name and attack value of the hero of the state of Wei .(AND)
There are two restrictions , The first is that the attack power needs to be higher than 200, Second, it must be a hero of the state of Wei , These two conditions are required to be true at the same time , So we use AND.
SELECT name,attack FROM hero WHERE attack>200 AND country=' State of Wei ';
2、 Find all the hero information of Shu and Wei .(OR)
It is worth noting that , This problem is a logical or relationship for computers , Find out the hero information of Shu OR The heroic information of the state of Wei , Because no hero is both Shu and Wei , If you use AND Connect and you can't find any heroes .
SELECT * FROM hero WHERE country=' Shu Kingdom ' OR country=' State of Wei ';
I'm the divider
5.3 Range comparison operator
The in scope comparison operator is actually a combination of the above two operators for logical functions , In order to facilitate the actual preparation of operations , Therefore, such an operator was born , There are three main forms , The types of values corresponding to various forms are different , Its general form is as follows :
WHERE Field name BETWEEN value 1 AND value 2 ; Values are numbers WHERE Field name IN ( value 1、 value 2、 value 3、···); The value can be a number , It can also be characters WHERE Field name NOT IN ( value 1、 value 2、 value 3、···); The value can be a number , It can also be characters
1、 Find the attack power between 100~200 Between the heroes of Shu .(BETWEEN)
The limitation here is 100~200, This is a range of values , So you can use BETWEEN To limit , similarly , You can also use the numerical comparison operator + Logical comparison operator implementation .
solution 1: Use in scope comparison operators SELECT * FROM hero WHERE (attack BETWEEN 100 AND 200) AND country=" Shu Kingdom ";
solution 2: Numeric comparison operators + Logical comparison operator SELECT * FROM hero WHERE (attack>100) AND (attack<200) AND country=' Shu Kingdom ';
2、 Find the heroine information of countries other than Shu and Wu .(NOT IN)
Here is to find the content beyond the restriction , Use NOT IN It is very convenient to limit .
SELECT * FROM hero WHERE (country NOT IN(" Shu Kingdom "," Wu kingdom ") )AND sex=" Woman ";
3、 lookup id by 1,3,5 Information about heroes of Shu and Diao Chan .(IN)
Here are three specific id Conditions , It is required to view within the given three ranges , Therefore use IN A more appropriate . Another thing worth noting is , There is an implied AND and OR Logic ,id The relationship with Shu heroes needs to be satisfied at the same time , So with AND Connect ; And Diao Chan is another information that needs to be met , It has nothing to do with the previous conditions , So with OR.
SELECT * FROM hero WHERE (id IN(1,3,5) AND country=" Shu Kingdom " ) OR name=" The sable cicada ";
I'm the divider
5.4 Match empty 、 Non air transport operators
Matching empty and non empty operations does not compare with empty through comparison operators , Instead, it is operated by a dedicated matching operator , Its general form is as follows :
WHERE Field name IS NULL; Empty operation WHERE Field name IS NOT NULL; Non empty operation
What we need to pay special attention to is ,NULL For null , It can only be used IS or IS NOT To match ; When we encounter an empty string " " when , We can only use character comparison operators to compare matches .
1、 Find the hero information whose name is null .( Null value )
SELECT * FROM hero WHERE name IS NULL;
2、 Find the hero information whose name is non null .( Non null value )
SELECT * FROM hero WHERE name IS NOT NULL;
3、 Find name as ' ' Hero information .( Empty string )
SELECT * FROM hero WHERE name=' ';
I'm the divider
5.5 Fuzzy comparison operator
Fuzzy comparison operator is an important operator , Just like the name , It can make fuzzy comparison , That is, in the process of query , When you only remember part of the information , Or remember when there is a common part , You can use fuzzy query , Its general format is as follows :
Format : WHERE Field name LIKE expression expression : _ Match a single character ; % matching 0 To more than one character
Most people may be confused about the above format and expression , Again , I saw it at first , But we can easily understand it through a few examples , believe me .
1、 Find all the hero information whose name is three words .
SELECT * FROM hero WHERE name LIKE '___' # There are three underlined here _ _ _
We know one Underline _ Represents matching a single character , Because we don't specify a specific hero here , Only the name of the hero is designated 3 A word , Therefore, the fuzzy comparison operator , In order to limit the search scope to 3 A hero of words , Let's use three Underline _ To match three characters , Finally, the following two results are obtained by matching .
2、 Find all names greater than 2 A hero of words .
SELECT * FROM hero WHERE name LIKE '__%'; # There are two here _, And a %
We need to find names greater than 2 A hero of characters , Because what is required here is greater than 2 Characters , So let's use two Underline _ , Then use another %. This is because using a % representative 0 To more than one character , The name character of the structure query can be at least 2 individual , Can be greater than 2 Characters .
边栏推荐
- Display picture of DataGridView cell in C WinForm
- February database ranking: how long can Oracle remain the first?
- 网络分层概念及基本知识
- Virtual machine startup prompt probing EDD (edd=off to disable) error
- 【ASM】字节码操作 ClassWriter 类介绍与使用
- 集成开发管理平台
- 华为认证云计算HICA
- MySQL advanced (index, view, stored procedure, function, password modification)
- [ciscn 2021 South China]rsa writeup
- Models used in data warehouse modeling and layered introduction
猜你喜欢
2021-03-22 "display login screen during recovery" can't be canceled. The appearance of lock screen interface leads to the solution that the remotely connected virtual machine can't work normally
Flink源码解读(二):JobGraph源码解读
案例:检查空字段【注解+反射+自定义异常】
Flink 解析(一):基础概念解析
Vscode matches and replaces the brackets
JVM garbage collector part 2
C# WinForm系列-Button简单使用
List集合数据移除(List.subList.clear)
Learn the wisdom of investment Masters
基于LNMP部署flask项目
随机推荐
Example of batch update statement combining update and inner join in SQL Server
Flink源码解读(一):StreamGraph源码解读
connection reset by peer
Some feelings of brushing leetcode 300+ questions
TCP connection is more than communicating with TCP protocol
【ASM】字节码操作 ClassWriter 类介绍与使用
04 products and promotion developed by individuals - data push tool
Re signal writeup
Selenium test of automatic answer runs directly in the browser, just like real users.
Automatic operation and maintenance sharp weapon ansible Foundation
Development and practice of lightweight planning service tools
07个人研发的产品及推广-人力资源信息管理系统
Akamai talking about risk control principles and Solutions
Total / statistics function of MySQL
Wu Jun trilogy insight (IV) everyone's wisdom
PySpark算子处理空间数据全解析(5): 如何在PySpark里面使用空间运算接口
MySQL报错解决
Coursera cannot play video
MySQL advanced (index, view, stored procedure, function, password modification)
Jetpack compose 1.1 release, based on kotlin's Android UI Toolkit