当前位置:网站首页>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 value2Usually 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 5BETWEEN 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 maxWhen 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 20090101BETWEEN 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=0Then 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 .
边栏推荐
- Method overloading and rewriting
- T430 toss and install OS majave 10.14
- 最新版抽奖盲盒运营版
- Grid connection - Analysis of low voltage ride through and island coexistence
- 【数据库原理及应用教程(第4版|微课版)陈志泊】【第三章习题】
- Sword finger offer14 the easiest way to cut rope
- Glide question you cannot start a load for a destroyed activity
- Xctf mobile--rememberother problem solving
- Integer case study of packaging
- When the R language output rmarkdown is in other formats (such as PDF), an error is reported, latex failed to compile stocks Tex. solution
猜你喜欢
![[review questions of database principles]](/img/c3/81d192a40bcc4f5d72fcbe76c708bb.png)
[review questions of database principles]

【数据挖掘复习题】
![[comprehensive question] [Database Principle]](/img/d7/8c51306bb390e0383a017d9097e1e5.png)
[comprehensive question] [Database Principle]
![[ArcGIS user defined script tool] vector file generates expanded rectangular face elements](/img/39/0b31290798077cb8c355fbd058e4d3.png)
[ArcGIS user defined script tool] vector file generates expanded rectangular face elements

Solve the problem of VI opening files with ^m at the end

并网-低电压穿越与孤岛并存分析

Xctf mobile--app2 problem solving

When the R language output rmarkdown is in other formats (such as PDF), an error is reported, latex failed to compile stocks Tex. solution

01 three solutions to knapsack problem (greedy dynamic programming branch gauge)
![[combinatorics] permutation and combination (the combination number of multiple sets | the repetition of all elements is greater than the combination number | the derivation of the combination number](/img/9d/6118b699c0d90810638f9b08d4f80a.jpg)
[combinatorics] permutation and combination (the combination number of multiple sets | the repetition of all elements is greater than the combination number | the derivation of the combination number
随机推荐
【习题七】【数据库原理】
Differences between initial, inherit, unset, revert and all
C graphical tutorial (Fourth Edition)_ Chapter 20 asynchronous programming: examples - cases without asynchronous
Sqoop1.4.4原生增量导入特性探秘
CNN MNIST handwriting recognition
C graphical tutorial (Fourth Edition)_ Chapter 18 enumerator and iterator: enumerator samplep340
The latest version of lottery blind box operation version
Leetcode234 palindrome linked list
基于同步坐标变换的谐波电流检测
C graphical tutorial (Fourth Edition)_ Chapter 17 generic: genericsamplep315
[Exercice 5] [principe de la base de données]
Node.js: express + MySQL的使用
ncnn神经网络计算框架在香橙派OrangePi 3 LTS开发板中的使用介绍
Two solutions of leetcode101 symmetric binary tree (recursion and iteration)
[exercise 7] [Database Principle]
十條職場規則
[ArcGIS user defined script tool] vector file generates expanded rectangular face elements
十条职场规则
【习题五】【数据库原理】
Ali & ant self developed IDE