当前位置:网站首页>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 .
边栏推荐
- 电压环对 PFC 系统性能影响分析
- Swift return type is a function of function
- 低代码平台国际化多语言(i18n)技术方案
- Everything comes to him who waits
- How to stand out quickly when you are new to the workplace?
- Four problems and isolation level of MySQL concurrency
- Low code platform international multilingual (I18N) technical solution
- [data mining review questions]
- Solve the problem of VI opening files with ^m at the end
- C graphical tutorial (Fourth Edition)_ Chapter 20 asynchronous programming: examples - using asynchronous
猜你喜欢
剑指 Offer 14- II. 剪绳子 II
【数据库原理及应用教程(第4版|微课版)陈志泊】【SQLServer2012综合练习】
阿里 & 蚂蚁自研 IDE
强大的头像制作神器微信小程序
Xctf mobile--app2 problem solving
How to convert a decimal number to binary in swift
Method overloading and rewriting
【ArcGIS自定义脚本工具】矢量文件生成扩大矩形面要素
Social community forum app ultra-high appearance UI interface
Analysis of the influence of voltage loop on PFC system performance
随机推荐
【数据库原理及应用教程(第4版|微课版)陈志泊】【第四章习题】
C graphical tutorial (Fourth Edition)_ Chapter 15 interface: interfacesamplep268
T430 toss and install OS majave 10.14
强大的头像制作神器微信小程序
Differences and connections between final and static
sitesCMS v3.0.2发布,升级JFinal等依赖
Swift5.7 扩展 some 到泛型参数
【習題七】【數據庫原理】
Kung Fu pays off, and learning is done
[exercise 6] [Database Principle]
[data mining review questions]
Redhat5 installing socket5 proxy server
Quickly learn member inner classes and local inner classes
Cache penetration and bloom filter
The best shortcut is no shortcut
Express abstract classes and methods
Project video based on Linu development
How to stand out quickly when you are new to the workplace?
node的ORM使用-Sequelize
2022-01-27 research on the minimum number of redis partitions