当前位置:网站首页>Mysqlbetween implementation selects the data range between two values
Mysqlbetween implementation selects the data range between two values
2022-07-03 12:58:00 【1024 questions】
BETWEEN example
BETWEEN Time date
BETWEEN Data comparison
MySQL IN usage
1.IN Operator is used for WHERE Expression
2.IN Subquery
3.IN Operator supplementary description
4. About IN The efficiency of operators
between and in The difference between
BETWEEN Operator is used for WHERE Expression , Select a data range between two values .
BETWEEN Same as AND Use together , The grammar is as follows :
WHERE column BETWEEN value1 AND value2WHERE column NOT BETWEEN value1 AND value2
Usually value1 It should be less than value2. When BETWEEN prefix NOT Operator , To express with BETWEEN The opposite meaning , That is, select values outside this range .
BETWEEN exampleselection uid stay 2 To 5 User data between :
SELECT * FROM user WHERE uid BETWEEN 2 AND 5
BETWEEN Time date BETWEEN AND It is often used to retrieve content within a time or date period , Here are some common BETWEEN Time date example :
// int Timestamp format , Inquire about 2008-08-08 20:00:00 To 2009-01-01 Data before zero SELECT * FROM table WHERE column_time BETWEEN 1218196800 AND 1230739199// DATE Format , Inquire about 2008-08-08 To 2009-01-01 Data before zero SELECT * FROM table WHERE column_time BETWEEN '2008-08-08' AND '2009-01-01'
// DATETIME Format , Inquire about 2008-08-08 20:00:00 To 2009-01-01 Data before zero SELECT * FROM table WHERE column_time BETWEEN '2008-08-08 20:00:00' AND '2008-12-31 23:59:59'
But for the data queried to the current time , It is recommended to use >= Operator :
// DATETIME Format , Inquire about 2008-08-08 20:00:00 Data up to the current moment SELECT * FROM table WHERE column_time >= '2008-08-08 20:00:00'
so , The same needs , Different field types , The writing method may be different .int The timestamp format has the best efficiency .
above BETWEEN Examples of , Although it's all SELECT Inquire about , but BETWEEN It can also be used for UPDATE、DELETE And so on WHERE Of expression SQL in .
BETWEEN Data comparisonBETWEEN It also has the function of data comparison , The grammar is as follows :
expr BETWEEN min AND max
When expr The value of the expression is greater than or equal to min And less than or equal to max when , BETWEEN The return value of is 1 , Otherwise return to 0 .
Take advantage of this feature , You can judge whether an expression or value is otherwise in a certain interval :
// return 0SELECT 1 BETWEEN 2 AND 3// return 1SELECT 'b' BETWEEN 'a' AND 'c'// Judge date range SELECT 20080808 BETWEEN 20080101 AND 20090101
BETWEEN And <、<=、>=、> The equal operator has similar functions in some cases , but BETWEEN The operation level is higher and the efficiency is better .
Of course, because BETWEEN There is a problem of boundary value, but it is not flexible , So different situations , What operator is used , Need to be treated specifically .
MySQL IN usage 1.IN Operator is used for WHERE ExpressionSupport multiple choices in the form of list items , The grammar is as follows :
WHERE column IN (value1,value2,...)WHERE column NOT IN (value1,value2,...)
When IN prefix NOT Operator , To express with IN The opposite meaning , That is to say, do not select .
IN Using examples
selection uid by 2、3、5 User data for :
SELECT * FROM user WHERE uid IN (2,3,5)
2.IN Subquery In more cases ,IN The value of the list item is ambiguous , It may be obtained through a sub query :
SELECT * FROM article WHERE uid IN(SELECT uid FROM user WHERE status=0)
In this SQL In the example , We have realized the detection of all States as 0 Users of ( May be banned ) All articles of . First, get all through a query status=0 Users of :
SELECT uid FROM user WHERE status=0
Then take the query result as IN List items to achieve the final query results , Note that the result returned in the subquery must be a field list item .
3.IN Operator supplementary descriptionIN List items not only support numbers , It also supports characters and even time and date types , And these different types of data items can be mixed and arranged without following column Keep consistent with the type of :
SELECT * FROM user WHERE uid IN(1,2,'3','c')
One IN Range comparison can only be performed on one field , If you want to specify more fields , have access to AND or OR Logical operators :
SELECT * FROM user WHERE uid IN(1,2) OR username IN('admin','5idev')
Use AND or OR After the logical operator ,IN It can also be combined with others such as LIKE、>=、= And so on .
4. About IN The efficiency of operatorsIf IN The list items of are certain , Then you can use multiple OR Instead of :
SELECT * FROM user WHERE uid IN (2,3,5)// Equivalent to :SELECT * FROM user WHERE (uid=2 OR aid=3 OR aid=5)
It is generally believed , If you are operating on an index field , Use OR Efficiency is higher than IN, But when the list item is uncertain ( If you need sub queries to get results ), Must use IN Operator . in addition , When the sub query table data is smaller than the main query , Also applicable IN The operator of the .
between and in The difference betweenIf you want to make the selected result within a certain range, you can use between and in These two keywords . In most cases, the reaction of both is the same , But if it is empty, the situation is different . as follows :
WHERE vbeln BETWEEN pa_vb_s AND pa_vb_e. WHERE VBELN IN pa_vb.
Their definition is as follows :
SELECT-OPTIONS pa_vb FOR vbrk-vbeln.PARAMETERS: pa_vb_s TYPE vbeln, pa_vb_e TYPE VBELN.
If they are all empty ,between Will limit select Statement makes it impossible to select a record , however in The opposite is true , This restriction will not work , All records will be selected . This is the biggest difference between the two .
But there is a little trap ,ranges Variables and select-options It feels the same . But in use select The performance of the sentence will be different . The code is as follows :
PARAMETERS: pa_vb_s TYPE vbeln, pa_vb_e TYPE VBELN. ranges pa_vb for VBRK-vbeln. pa_vb-sign = 'I'. pa_vb-option = 'BT'. PA_VB-LOW = PA_VB_S. PA_VB-HIGH = PA_VB_E. APPEND pa_vb.
At this time, if you use just select Sentence use in To limit , You can't choose any ! At first, I didn't understand why this happened , The tracker found that append this sentence .
If you use selct-options This sentence , If you don't fill anything in the selection box , that select-options This ragnes There is a null value in the variable , That is, the number of records in the internal table is 0! It's equivalent to not having append.
But we customize ragnes Variables will append Null value , Even if there's no input .
This is the same as between The effect is the same . If you want to be with me selct-options Just write it like this .
IF PA_VB_S IS NOT INITIAL or PA_VB_E IS NOT INITIAL. APPEND pa_vb. ENDIF.
The above is personal experience , I hope I can give you a reference , I also hope you can support the software development network .
边栏推荐
- 关于CPU缓冲行的理解
- 【数据库原理及应用教程(第4版|微课版)陈志泊】【第六章习题】
- Keep learning swift
- Ten workplace rules
- [Exercice 5] [principe de la base de données]
- SQL learning notes (I)
- (最新版) Wifi分销多开版+安装框架
- GaN图腾柱无桥 Boost PFC(单相)七-PFC占空比前馈
- Loan calculator my pressure is high
- Bert running error: attributeerror: module'tensorflow contrib. tpu' has no attribute 'InputPipelineConfig'
猜你喜欢
【综合题】【数据库原理】
Two solutions of leetcode101 symmetric binary tree (recursion and iteration)
剑指 Offer 12. 矩阵中的路径
Low code platform international multilingual (I18N) technical solution
IDEA 全文搜索快捷键Ctr+Shift+F失效问题
Xctf mobile--app2 problem solving
[network counting] Chapter 3 data link layer (2) flow control and reliable transmission, stop waiting protocol, backward n frame protocol (GBN), selective retransmission protocol (SR)
[review questions of database principles]
Differences between initial, inherit, unset, revert and all
【计网】第三章 数据链路层(2)流量控制与可靠传输、停止等待协议、后退N帧协议(GBN)、选择重传协议(SR)
随机推荐
Analysis of the influence of voltage loop on PFC system performance
最新版盲盒商城thinkphp+uniapp
Approve iPad, which wants to use your icloud account
(latest version) WiFi distribution multi format + installation framework
Ali & ant self developed IDE
Differences between initial, inherit, unset, revert and all
Everything comes to him who waits
Deeply understand the mvcc mechanism of MySQL
【数据库原理及应用教程(第4版|微课版)陈志泊】【第三章习题】
[combinatorics] permutation and combination (multiple set permutation | multiple set full permutation | multiple set incomplete permutation all elements have a repetition greater than the permutation
【数据库原理复习题】
Keep learning swift
context. Getexternalfilesdir() is compared with the returned path
Node.js: express + MySQL的使用
[problem exploration and solution of one or more filters or listeners failing to start]
How to stand out quickly when you are new to the workplace?
【習題七】【數據庫原理】
Integer case study of packaging
Sqoop1.4.4原生增量导入特性探秘
Swift return type is a function of function