当前位置:网站首页>[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 .
边栏推荐
- 基于LNMP部署flask项目
- MySQL Advanced (index, view, stored procedures, functions, Change password)
- [ciscn 2021 South China]rsa writeup
- JVM 垃圾回收器之Serial SerialOld ParNew
- mysql 基本增删改查SQL语句
- 关于Selenium启动Chrome浏览器闪退问题
- The NTFS format converter (convert.exe) is missing from the current system
- Quick start of Hongmeng system
- yarn : 无法加载文件 D:\ProgramFiles\nodejs\yarn.ps1,因为在此系统上禁止运行脚本
- [reverse intermediate] eager to try
猜你喜欢
TCP connection is more than communicating with TCP protocol
CTF reverse entry question - dice
网络分层概念及基本知识
Re signal writeup
Wordcloud colormap color set and custom colors
基于LNMP部署flask项目
Uipath browser performs actions in the new tab
连接局域网MySql
[reverse] repair IAT and close ASLR after shelling
JVM 垃圾回收器之Serial SerialOld ParNew
随机推荐
C#WinForm中的dataGridView滚动条定位
Detailed explanation of data types of MySQL columns
TCP connection is more than communicating with TCP protocol
Interpretation of Flink source code (I): Interpretation of streamgraph source code
Automatic operation and maintenance sharp weapon ansible Foundation
Akamai浅谈风控原理与解决方案
Xin'an Second Edition; Chapter 11 learning notes on the principle and application of network physical isolation technology
yarn : 无法加载文件 D:\ProgramFiles\nodejs\yarn.ps1,因为在此系统上禁止运行脚本
Display picture of DataGridView cell in C WinForm
CTF reverse entry question - dice
PySpark算子处理空间数据全解析(4): 先说说空间运算
Based on infragistics Document. Excel export table class
Example of batch update statement combining update and inner join in SQL Server
Junit单元测试
04个人研发的产品及推广-数据推送工具
Quick start of Hongmeng system
远程代码执行渗透测试——B模块测试
mysql高级(索引,视图,存储过程,函数,修改密码)
04 products and promotion developed by individuals - data push tool
Serial serialold parnew of JVM garbage collector