Summary:
- like 加 ‘%ss%’
- not like
- mybatis uses concat
- Tsuhaifu
SQL LIKE operator
LIKE operator is used in the WHERE clause specifies the column search pattern.
LIKE operator
LIKE operator is used in the WHERE clause specifies the column search pattern.
SQL LIKE operator syntax
SELECT column_name(s)
FROM table_name
WHERE column_name LIKE pattern
Now we want to choose people living in cities beginning with "N" in from the top of the "Persons" table:
We can use the following SELECT statement:
SELECT * FROM Persons
WHERE City LIKE 'N%'
Next, we want to choose people live in cities with "g" in the end from the "Persons" table:
We can use the following SELECT statement:
SELECT * FROM Persons
WHERE City LIKE '%g'
SELECT * FROM Persons
WHERE City LIKE '%lon%'
The use mybatis
concat splicing
concat(concat (’%’,#{foldername}),’%’)
foldername like concat(concat ('%',#{foldername}),'%')
SQL wildcard
SQL wildcard
When data in the search database, SQL wildcards can substitute one or more characters.
It must be used with SQL wildcard LIKE operator.
In SQL, you can use the following wildcards:
Tsuhaifu | description |
---|---|
% | Replace one or more characters |
_ | Replace only one character |
[charlist] | Character any single character column |
[^ Charlist] or [! Charlist] | Not in the character of any single character column |
The original table (used in the example):
Persons 表:
Id LastName FirstName Address City
1 Adams John Oxford Street London
2 Bush George Fifth Avenue New York
3 Carter Thomas Changan Street Beijing
Use% wildcard
Now we want to choose people living in cities beginning with "Ne" in from the top of the "Persons" table:
We can use the following SELECT statement:
SELECT * FROM Persons
WHERE City LIKE 'Ne%'
The results set:
Id LastName FirstName Address City
2 George Bush Fifth Avenue New York
Example 2
Next, we want to choose people live in the city include "lond" from the inside "Persons" table:
We can use the following SELECT statement:
SELECT * FROM Persons
WHERE City LIKE '%lond%'
结果集:
Id LastName FirstName Address City
1 Adams John Oxford Street London
_ Use Wildcards
Example 1
Now we want to select the name after the first character from above "Persons" table is "eorge" people:
We can use the following SELECT statement:
SELECT * FROM Persons
WHERE FirstName LIKE '_eorge'
The results set:
Id LastName FirstName Address City
2 George Bush Fifth Avenue New York
Example 2
Next, we want to choose from the "Persons" table in this record last name begins with "C", then any character, then " r ", then any character, then" er ":
We can use the following SELECT statement:
SELECT * FROM Persons
WHERE LastName LIKE 'C_r_er'
结果集:
Id LastName FirstName Address City
3 Carter Thomas Changan Street Beijing
Using [the charlist] wildcard
Example 1
Now we want to choose the city to live with "A" or "L" or "N" from the beginning of man above "Persons" table:
We can use the following SELECT statement:
SELECT * FROM Persons
WHERE City LIKE '[ALN]%'
The results set:
Id LastName FirstName Address City
1 Adams John Oxford Street London
2 Bush George Fifth Avenue New York
Example 2
Now we want the city from above "Persons" table, choose to live not to "A" or "L" or " N "at the beginning of people:
We can use the following SELECT statement:
SELECT * FROM Persons
WHERE City LIKE '[!ALN]%'
结果集:
Id LastName FirstName Address City
3 Carter Thomas Changan Street Beijing