当前位置:网站首页>Fuzzy query of SQL
Fuzzy query of SQL
2022-07-29 04:10:00 【Arvin L】
When making a database query , There are complete query and fuzzy query .
General fuzzy query statements are as follows :
| SELECT Field FROM surface WHERE A field Like Conditions |
It's about conditions ,SQL Four matching patterns are provided :
1,% : Express arbitrarily 0 Characters or more . Matches characters of any type and length , In some cases, it's Chinese , Please use two percent signs (%%) Express .
such as SELECT * FROM [user] WHERE u_name LIKE '% 3、 ... and %'
It will put u_name by “ Zhang San ”,“ Zhang maosan ”、“ Three legged cat ”,“ Tang Sanzang ” Wait, there are “ 3、 ... and ” I've got all my records .
in addition , If you need to find out u_name There is already “ 3、 ... and ” And then there is “ cat ” The record of , Please use and Conditions
SELECT * FROM [user] WHERE u_name LIKE '% 3、 ... and %' AND u_name LIKE '% cat %'
If you use SELECT * FROM [user] WHERE u_name LIKE '% 3、 ... and % cat %'
Although we can find out “ Three legged cat ”, But you can't find the right ones “ Zhang maosan ”.
2,_ : Represents any single character . Match a single arbitrary character , It is often used to limit the character length of an expression :
such as SELECT * FROM [user] WHERE u_name LIKE '_ 3、 ... and _'
Just find out “ Tang Sanzang ” such u_name It's three words and the middle word is “ 3、 ... and ” Of ;
Another example SELECT * FROM [user] WHERE u_name LIKE ' 3、 ... and __';
Just find out “ Three legged cat ” such name It's three words and the first word is “ 3、 ... and ” Of ;
3,[ ] : Represents one of the characters listed in brackets ( Like regular expressions ). Specify a character 、 String or range , Require the matched object to be any of them .
such as SELECT * FROM [user] WHERE u_name LIKE '[ Zhang liwang ] 3、 ... and '
Will find out “ Zhang San ”、“ Li San ”、“ The king of the three ”( instead of “ Zhang Li, Wang San ”);
Such as [ ] There's a series of characters in it (01234、abcde And so on. ) It can be abbreviated as “0-4”、“a-e”
SELECT * FROM [user] WHERE u_name LIKE ' The old [1-9]'
Will find out “ The old 1”、“ The old 2”、……、“ The old 9”;
4,[^ ] : Represents a single character that is not listed in parentheses . Its value and [] identical , But it requires the matched object to be any character other than the specified character .
such as SELECT * FROM [user] WHERE u_name LIKE '[^ Zhang liwang ] 3、 ... and '
Will find out the last name “ Zhang ”、“ Li ”、“ king ” Of “ Zhao San ”、“ Sun San ” etc. ;
SELECT * FROM [user] WHERE u_name LIKE ' The old [^1-4]';
Will exclude “ The old 1” To “ The old 4”, seek “ The old 5”、“ The old 6”、……
5, When the query contains wildcards
Because of wildcards , Causes us to query for special characters “%”、“_”、“[” The statement cannot be implemented properly , And use special characters with “[ ]” You can query it normally . So let's write the following function :
function sqlencode(str) str=replace(str,"[","[[]") ' This sentence must come first str=replace(str,"_","[_]") str=replace(str,"%","[%]") sqlencode=str end function |
Before querying, the string to be queried can be processed by this function , And when you connect to a database on a web page and use such query statements, you should pay attention to :
Such as Select * FROM user Where name LIKE ' The old [^1-4]'; above 《'》 The old [^1-4]《'》 It has to have single quotation marks , Don't forget , I often forget !
access
Write in recent days Web The program uses Access Fuzzy query of , stay Acces I can't find any records in the code , Later, I got up. It turned out Acess and SqlServer Fuzzy query has special
Conditions : Lookup table A Of Name The field includes "B" Remember when
stay Access Code in :
1 Select * from a where name like '*b*'Sql Server Query analyzer code
Select * from a where name like '%b%' Then you will find Access Relevant records can be found in , But put '*' Bi Cheng '%' I can't find , as a result of Access The fuzzy query is '?','*'
and Sql server Dissimilarity
The above is just the code in the database , If you want to write it in the program, you can't use .'*' 了 , Still need to use '%'
Program :
strSql="select * from a where name like '%b%'" So if a friend likes to test the code in the database as much as I do , Then pay attention !!
----------------------------------------------------------------------------------------------------------
SQL Fuzzy query , Use like Compare keywords , add SQL The wildcards in , Please refer to the following :
1、LIKE'Mc%' Will search with letters Mc All strings at the beginning ( Such as McBadden).
2、LIKE'%inger' Will search with letters inger All strings at the end ( Such as Ringer、Stringer).
3、LIKE'%en%' Will search to include letters anywhere en All strings of ( Such as Bennet、Green、McBadden).
4、LIKE'_heryl' Will search with letters heryl All six letter names at the end ( Such as Cheryl、Sheryl).
5、LIKE'[CK]ars[eo]n' The following string will be searched :Carsen、Karsen、Carson and Karson( Such as Carson).
6、LIKE'[M-Z]inger' Search as string inger ending 、 From the M To Z All names beginning with any single letter of ( Such as Ringer).
7、LIKE'M[^c]%' Will search with letters M start , And the second letter is not c All the names of ( Such as MacFeather).
-------------------------------------------------
Next The query string of face is what I wrote before , According to the variable zipcode_key In the zip code table zipcode Query the corresponding data in , This sentence is a judgment variable zipcode_key Query statement when it is non numeric , use % To match any length of string , Address from table 、 City 、 Save three columns to query all data items containing keywords , And by province 、 City 、 Address sorting . This is a simple example , As long as you understand the method, you can write more Complex query statements .
sql = "select * from zipcode where (address like'%" & zipcode_key & "%') or (city like'%" & zipcode_key & "%') or (province like'%" & zipcode_key & "%') order by province,city,address
Examples of using fuzzy queries in stored procedures :
SELECT * FROM Questions where QTitle like ' % [ '+ @KeyWord +' ] % ' and IsFinish = @IsFinsih
Pairs of square brackets in a statement Is the key to writing format .
边栏推荐
- C language to achieve three chess game (detailed explanation)
- 企业网的三层架构
- Is the browser multi process or single process?
- After I get the winfrom specific control ID from the database, I need to find the corresponding control through this ID and assign a value to the text text of the control. What should I do
- The data source is SQL server. I want to configure the incremental data of the last two days of the date field updatedate to add
- 全屋WiFi方案:Mesh路由器组网和AC+AP
- Press the missing number of interview question 17.04 | | 260. the number that appears only once (including bit operation knowledge points)
- SQL server how to judge when the parameter received by the stored procedure is of type int?
- Compilation and linking
- UnicodeDecodeError: ‘ascii‘ codec can‘t decode byte 0x90 in position 614: ordinal not in range(128)
猜你喜欢

AssertionError(“Torch not compiled with CUDA enabled“)

Lua language (stm32+2g/4g module) and C language (stm32+esp8266) methods of extracting relevant data from strings - collation

rman不标记过期备份

The solution of porting stm32f103zet6 program to c8t6+c8t6 download program flash timeout

LDP --- 标签分发协议

C language to achieve three chess game (detailed explanation)

关于双指针的思想总结
![[BGP] small scale experiment](/img/58/877e5e454e9bab9d1bccb8fdd3b04d.png)
[BGP] small scale experiment

Install the laser of ROS_ scan_ Problems encountered in match library (I)

Simple cases of inner connection and left connection
随机推荐
Install the laser of ROS_ scan_ Problems encountered in match library (I)
路由 知识
[paper translation] vectornet: encoding HD maps and agent dynamics from vectorized representation
LDP --- 标签分发协议
Lucifer 98 life record ing
数据库SQL语句实现数据分解的函数查询
SQL语句 关于字段转换怎么写
Routing knowledge
请问,在sql client中,执行insert into select from job时,如何单
flink-sql 如何设置 sql执行超时时间
HCIP BGP
谁能详细说下mysqlRC下的半一致读和怎么样减少死锁概率?
The output comparison function of Tim is introduced in detail through PWM breathing lamp and PWM controlled DC motor
MySQL Part 4 (end)
C语言实现三子棋游戏(详解)
数据源是SQL server ,我要配置日期字段 updateDate 最后两天日期的增量数据,做增
Pointer of pointer???...
Use case of arrow function of new features in ES6
Simple cases of inner connection and left connection
Is there any way for Youxuan database to check the log volume that the primary cluster transmits to the standby cluster every day?