SQL SQL之类的搜索字符串以

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14908142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 13:43:09  来源:igfitidea点击:

SQL like search string starts with

sql

提问by 0xSina

Learning SQL. Have a simple table games with field title. I want to search based on title. If I have a game called Age of Empires III: Dynasties, and I use LIKEwith parameter Age of Empires III: Dynasties, everything works fine, the search returns the record with that name. But if I search with Age of Empires III, it doesn't return any records:

学习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');

This doesn't return anything. Should I be using something else instead of LIKE?

这不会返回任何东西。我应该使用其他东西而不是LIKE吗?

I am using MySQL.

我正在使用 MySQL。

回答by mr_eclair

SELECT * from games WHERE (lower(title) LIKE 'age of empires III');

The above query doesn't return any rows because you're looking for 'age of empires III' exact string which doesn't exists in any rows.

上面的查询不返回任何行,因为您正在寻找任何行中都不存在的“帝国时代 III”确切字符串。

So in order to match with this string with different string which has 'age of empires'as substring you need to use '%your string goes here%'

因此,为了将这个字符串与具有'age of empires'子字符串的不同字符串匹配,您需要使用'%your string goes here%'

More on mysql string comparision

更多关于mysql 字符串比较

You need to try this

你需要试试这个

SELECT * from games WHERE (lower(title) LIKE '%age of empires III%');

In Like '%age of empires III%'this will search for any matching substring in your rows, and it will show in results.

In Like '%age of empires III%'this 将在您的行中搜索任何匹配的子字符串,并将显示在结果中。

回答by misha

You need to use the wildcard %:

您需要使用通配符%

SELECT * from games WHERE (lower(title) LIKE 'age of empires III%');

回答by Dhwani

COLLATE UTF8_GENERAL_CIwill work as ignore-case. USE:

COLLATE UTF8_GENERAL_CI将用作忽略大小写。用:

SELECT * from games WHERE title COLLATE UTF8_GENERAL_CI LIKE 'age of empires III%';

or

或者

SELECT * from games WHERE LOWER(title) LIKE 'age of empires III%';

回答by matepal297

Aside from using %, age of empires IIIto lower case is age of empires iiiso your query should be:

除了使用%,age of empires III小写外,age of empires iii您的查询应该是:

select *
from games
where lower(title) like 'age of empires iii%'