To implement fuzzy query in SQL, we only need to use like, and some parameters such as %? are included in the middle? And so on. Here is an instance. The LIKE operator is used to search for the specified mode in the WHERE clause. SQLLIKE operator syntax SELECTcolumn_name (s) FROMtable_nameWHEREcolumn_nameLIKE1 & gt; CREATETABLEem we only need to use like to implement fuzzy query in SQL, with some parameters such as %? And so on. Here is an instance.
LIKE Operator
The LIKE operator is used to search for the specified mode in the WHERE clause.
SQL LIKE operator syntax
SELECT column_name (s) FROM table_nameWHERE column_name LIKE
1> create table employee (emp_no integer not null,
2> emp_fname CHAR (20) not null,
3> emp_lname CHAR (20) not null,
4> dept_no CHAR (4) NULL)
5> GO
1> insert into employee values (1, 'Matthew ', 'Smith', 'd3 ')
2> insert into employee values (2, 'ann ', 'Jones', 'd3 ')
3> insert into employee values (3, 'john', 'barrimore ', 'd1 ')
4> insert into employee values (4, 'James ', 'James', 'd2 ')
5> insert into employee values (5, 'elsa ', 'bertoni', 'd2 ')
6> insert into employee values (6, 'elke', 'hansel ', 'd2 ')
7> insert into employee values (7, 'sybill ', 'moser', 'd1 ')
8> GO
> SELECT * FROM employee WHERE emp_fname not like '% N'
4> GO
Emp_no emp_fname emp_lname dept_no
----------------------------------------------------------
1 Matthew Smith d3
4 James d2
5 Elsa Bertoni d2
6 Elke Hansel d2
7 Sybill Moser d1
Instance 2
2> SELECT *
3> FROM Employee
4> WHERE Name LIKE "% [k-l] %"
5> GO
ID name salary start_date city region
-----------------------------------------------------------------------
3 Celia 24020 00:00:00. 000 Toronto W
4 Linda 40620 00:00:00. 000 New York N
7 Alison 90620 00:00:00. 000 New York W
About like %
# % Represents any number of characters
* From user where username like '% huxiao ';
Select * from user where username like 'huxiao % ';
Select * from user where username like '% huxiao % ';
# % Represents a character
Select * from user where username like '_';
Select * from user where username like '______';
Select * from user where username like 'huxia _';
Select * from user where username like 'H _ xiao ';
# What if I want to check % or? When escape is used, the % or _ after the escape character is not used as a wildcard. Note that the % and _ without the escape character are still used as wildcards.
Select username from gg_user where username like '% xiao/_ %' escape '/';
Select username from gg_user where username like '% xiao/%' escape '/';
About wildcards
'A _ Z': All strings starting with 'a and ending with 'Z. Both 'abz' and 'a2z' comply with this mode, while 'akkz' does not (because there are two original fonts between A and Z, rather than one original ).
'Abc % ': All strings starting with 'abc. For example, both 'abc' and 'abcabc' comply with this rule.
'% Xyz': All strings ending with 'xyz. For example, both 'wxyz' and 'zzxyz' comply with this rule.
'% AN %': All strings containing the 'AN' format. For example, both 'Los Angeles' and 'san FRANCISCO 'comply with this rule.
In SQL, you can use the following wildcard characters:
Wildcard |
Description |
% |
Replace one or more characters |
_ |
Replace only one character |
[Charlist] |
Any single character in the character Column |
[^ Charlist] Or [! Charlist] |
Not in the character Column |