当前位置:网站首页>[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 characters1、 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 operationWhat 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 characterMost 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 .
边栏推荐
- [reverse primary] Unique
- Kali2021 installation and basic configuration
- 07个人研发的产品及推广-人力资源信息管理系统
- 分布式(一致性协议)之领导人选举( DotNext.Net.Cluster 实现Raft 选举 )
- connection reset by peer
- [reverse] repair IAT and close ASLR after shelling
- 应用服务配置器(定时,数据库备份,文件备份,异地备份)
- Display picture of DataGridView cell in C WinForm
- 04 products and promotion developed by individuals - data push tool
- PySpark算子处理空间数据全解析(4): 先说说空间运算
猜你喜欢

分布式(一致性协议)之领导人选举( DotNext.Net.Cluster 实现Raft 选举 )

EasyRE WriteUp

Program counter of JVM runtime data area

Junit单元测试

Solr appears write Lock, solrexception: could not get leader props in the log

自动化运维利器ansible基础

The NTFS format converter (convert.exe) is missing from the current system

当前系统缺少NTFS格式转换器(convert.exe)

Grafana 9 正式发布,更易用,更酷炫了!

Final review of information and network security (based on the key points given by the teacher)
随机推荐
04个人研发的产品及推广-数据推送工具
Flink parsing (VI): savepoints
06个人研发的产品及推广-代码统计工具
Program counter of JVM runtime data area
[VNCTF 2022]ezmath wp
Xin'an Second Edition; Chapter 11 learning notes on the principle and application of network physical isolation technology
Interpretation of Flink source code (II): Interpretation of jobgraph source code
List集合数据移除(List.subList.clear)
信息与网络安全期末复习(基于老师给的重点)
分布式(一致性协议)之领导人选举( DotNext.Net.Cluster 实现Raft 选举 )
mysql 基本增删改查SQL语句
Flink 解析(五):State与State Backend
Redis installation on centos7
07 personal R & D products and promotion - human resources information management system
关于Selenium启动Chrome浏览器闪退问题
Shawshank's sense of redemption
pip install pyodbc : ERROR: Command errored out with exit status 1
Deploy flask project based on LNMP
[reverse primary] Unique
Quick start of Hongmeng system














