当前位置:网站首页>MySQL data query (where)
MySQL data query (where)
2022-07-27 23:39:00 【Yu'an.112】
WHERE The clause must be immediately followed by FROM After Clause , stay WHERE clause , Use a condition from FROM Select a row from the intermediate result of the clause . Grammar format :WHERE < Decision operation >
Decision operation : The result is true、false or unknown
WHERE Clauses will match FROM The intermediate result of the clause is judged row by row , When conditions are ture when , One line is included in WHERE Intermediate result set of clause .
stay SQL in , Returns the logical value (true or false) Operators or keywords of can be called predicates , Decision operation includes comparison operation 、 Pattern matching 、 Range comparison 、 Null value comparison and subquery .
1、 Comparison operations
The comparison operator is used to compare two expression values ,MySQL The supported comparison operators are :=( be equal to ),<( Less than ),<=( Less than or equal to ),>( Greater than ),>=( Greater than or equal to ),<=>( Equal or equal to nothing ),<>( It's not equal to ),!=( It's not equal to ).
Expression is division TEXT and BLOB Expressions outside of type .
When both expression values are not null (NULL) when , except “<=>” Operator , Other comparison operations return logical values true( really ) or false( false ). When one of the two expression values is null or both are null , Will return unknown.
example : Inquire about Bookstore database book The title of the book in the table is “ Web programming ” The record of
SELECT *
FROM book
WHERE Title =' Web programming ';example : Inquire about Book The unit price in the table is greater than 30 Books of
SELECT *
FROM Book
WHERE The unit price >30;MySQL There is one special The equal operator of “<=>”, When two expressions are equal to each other or both are equal to null , The value of the operation is true, When there is a null value or both non null values but not equal, the result of this condition is false, But not unknown The situation of .
2、 Logical operations
Logical operation can pass the results of multiple decision operations through logical operators (AND、OR、XOR and NOT) Form more complex query conditions .
Logical operators are used to test a condition , The result of operation is true(1) or false(0).MySQL The logical operators provided are :
not or !: Logic is not
or or ||: Logic or
and or &&: Logic and
xor: Logical XOR
The result of logical operation is 1 or 0, respectively true and false.
3、 Pattern matching
LIKE The operator is used to indicate whether a string matches the specified string , Its operands can be char、varchar、text、datetime And so on , Returns the logical value true or false.
Grammar format : expression [not] LIKE expression
Use LIKE When pattern matching , Special symbols are often used _ and % Fuzzy query ,% representative 0 Characters or more ,_ Single character .
because MySQL Case insensitive by default , To be case sensitive, you need to change the proofing rules of the character set .
example : Inquire about Members Last name in the table “ Zhang ” Member's ID number 、 full name 、 Registration time
SELECT ID number , Membership name , Registration time
FROM Members
WHERE Membership name LIKE' Zhang %';example : Inquire about book The book number in the table is the penultimate 2 Position as D Book number and title
SELECT Book number , Title
FROM book
WHERE Book number LIKE'%D_';To find one or all of the special symbols (_ and %), An escape character must be used . When looking for underscores _ when , have access to ESCAPE'#' To define # For escape characters , such , In the statement # hinder _ It loses its original special significance , Underline that is considered normal _.
example : Inquire about book Books with underlined titles in the table .
SELECT Book number , Title
FROM book
WHERE Title LIKE'%#_%'ESCAPE'#';4、 Range comparison
There are two keywords used for range comparison , Namely BETWEEM and IN
When the condition to be queried is a range of values , have access to BETWEEN keyword .BETWEEN Keywords indicate the query scope , The format is : expression [NOT] BETWEEN expression 1 AND expression 2
example : Inquire about Book In the table 2003 The situation of books published in
SELECT *
FROM Book
WHERE Publication date BETWEEN'2003-1-1' AND '2003-12-31';
# or
SELECT *
FROM Book
WHERE Publication date >='2003-1-1' AND Publication date <='2003-12-31';example : To find out Book It's not in the table 2003 The situation of all books published in , Use NOT
SELECT *
FROM Book
WHERE Publication date NOT BETWEEN '2003-1-1' AND '2003-12-31';
# or
SELECT *
FROM Book
WHERE Publication date <='2003-1-1' OR Publication date >='2003-12-31';Use IN Keywords can specify a value table , All possible values are listed in the value table , When it matches any one in the value table , Return true, Otherwise return to false.
Grammar format : expression [NOT] IN( Subquery | expression 1[,... expression n])
IN The most commonly used keywords are expression sub queries , It can also be used for OR operation .
example : Inquire about Book In the table “ Higher Education Press ”,“ Peking University press ” and “ People's University Press ” Published books
SELECT * FROM Book
WHERE Press. IN(' Higher Education Press ',' Peking University press ',' People's University Press ');
# or
SELECT * FROM Book
WHERE Press. =' Higher Education Press '
OR Press. =' Peking University press '
OR Press. =' People's University Press ';5、 Empty value comparison
When it is necessary to determine whether the value of an expression is null , Use IS NULL keyword .
Grammar format : expression IS [NOT] NULL
If the value of the expression is null , return true, Otherwise return to false, When using not when , The result is just the opposite .
example : Inquire about Sell Order records in the table that have not been shipped
SELECT *
FROM Sell
WHERE Whether to ship the goods or not IS NULL;边栏推荐
- CaEGCN: Cross-Attention Fusion based Enhanced Graph Convolutional Network for Clustering 2021
- Desai wisdom number - other charts (parallel coordinate chart): family's willingness to allocate assets in the future
- What is the difference between concurrency and parallelism?
- 携手长江存储,江波龙推出全球最小扩展卡
- JUC工具包学习
- 回Mixlab三天,“创造力团队”治好了我的精神内耗
- 新技术引领大中型企业营销新变革,用友BIP CRM重磅发布!
- Can Siemens PLC collect analog data of multiple slave stations in real time and wirelessly?
- 360 was selected into the panorama of China's security industry, and isc2022 talked about the direction of security services in 63 fields
- reduce错误示范
猜你喜欢

2022夏暑假每日一题(五)

小程序容器技术超有料,可以让移动研发效率大幅提升

面试官:说一下网络数据传输的具体流程

CaEGCN: Cross-Attention Fusion based Enhanced Graph Convolutional Network for Clustering 2021

Learn more about xxE injection

Preliminary understanding of Panda3D audio and advanced interactive components

深入了解 XXE 注射

Sudden, wechat important notice

2022年土木,建筑与环境工程国际会议(ICCAEE 2022)

寻找和利用 XXE – XML 外部实体注入
随机推荐
My annual salary is 1million, and I don't have clothes more than 100 yuan all over my body: saving money is the top self-discipline
怎么使用C# Winform实现复制文件显示进度
Solve 5g pain points, Meizu 17 smart 5g fast and stable technology release
Sudden, wechat important notice
Software test function test full set of common interview questions [function test] interview summary 4-2
Current situation and future of Nb IOT industry: cross the threshold of 100million shipments and rush to 5g connection!
Disable caching with meta HTML tags in all browsers
Implicit indicators for evaluating the advantages and disadvantages of automated testing
编辑复制粘贴判定问题(bug?),所见即所得显示符号问题反馈。
Nature综述:微生物群落形成过程中的优先效应
Application skills of AWS dynamodb
图基本知识代码
Reinforcement learning - pytorch realizes advantage actor critical (A2C)
Figure basic knowledge code
记录一下使用R语言中关于formatC的错误
The technology of applet container is very promising, which can greatly improve the efficiency of mobile R & D
Elk log analysis system installation and deployment
Desai wisdom number - other charts (parallel coordinate chart): family's willingness to allocate assets in the future
2022/7/26
Visual display method of machine learning project