学习SQL.有一个简单的桌面游戏与字段标题.我想根据标题进行搜索.如果我有一个名为的游戏Age of Empires III: Dynasties,并且我使用LIKE参数Age of Empires III: Dynasties,一切正常,搜索将返回具有该名称的记录.但如果我搜索Age of Empires III,它不会返回任何记录:
SELECT * from games WHERE (lower(title) LIKE 'age of empires III');
Run Code Online (Sandbox Code Playgroud)
这不会返回任何东西.我应该使用其他东西而不是LIKE吗?
我正在使用MySQL.
mr_*_*air 51
SELECT * from games WHERE (lower(title) LIKE 'age of empires III');
Run Code Online (Sandbox Code Playgroud)
上面的查询不会返回任何行,因为您正在寻找"帝国时代III"的精确字符串,这在任何行中都不存在.
所以为了匹配这个字符串与不同的字符串,'age of empires'你需要使用子字符串'%your string goes here%'
更多关于mysql字符串比较
你需要尝试这个
SELECT * from games WHERE (lower(title) LIKE '%age of empires III%');
Run Code Online (Sandbox Code Playgroud)
在Like中,'%age of empires III%'将搜索行中任何匹配的子字符串,它将显示在结果中.
mis*_*sha 34
您需要使用通配符%:
SELECT * from games WHERE (lower(title) LIKE 'age of empires III%');
Run Code Online (Sandbox Code Playgroud)
mat*_*297 11
除了使用%,age of empires III小写外,age of empires iii您的查询应该是:
select *
from games
where lower(title) like 'age of empires iii%'
Run Code Online (Sandbox Code Playgroud)
小智 9
WHERE CustomerName LIKE 'a%' --查找以“a”开头的任何值
WHERE CustomerName LIKE '%a' --查找以“a”结尾的任何值
WHERE CustomerName LIKE '%or%' --查找任何位置有“or”的任何值
WHERE CustomerName LIKE '_r%' --查找第二个位置有“r”的任何值
WHERE CustomerName LIKE 'a__%' --查找以“a”开头且长度至少为 3 个字符的任何值
WHERE ContactName LIKE 'a%o' --查找以“a”开头并以“o”结尾的任何值
-- 不区分大小写 2 SELECT * FROM my_table WHERE upper(my_column) LIKE 'SEARCHED %'; - 以。。开始
3 SELECT * FROM my_table WHERE upper(my_column) LIKE '% SEARCHED'; - 以。。结束
4 SELECT * FROM my_table WHERE upper(my_column) LIKE '%SEARCHED%'; -- 包含
COLLATE UTF8_GENERAL_CI将作为忽略大小写。使用:
SELECT * from games WHERE title COLLATE UTF8_GENERAL_CI LIKE 'age of empires III%';
Run Code Online (Sandbox Code Playgroud)
或者
SELECT * from games WHERE LOWER(title) LIKE 'age of empires III%';
Run Code Online (Sandbox Code Playgroud)