[英]How to query first letter of each character?
I have a table which has three columns: firstname, middlename, lastname. 我有一个包含三列的表:名字,中间名,姓氏。 Now, when I type the first letter of each character, the query have to return correctly results.
现在,当我键入每个字符的第一个字母时,查询必须正确返回结果。
Example: My database has one record: 示例:我的数据库有一条记录:
When I type the input string as below (just only bold string), the returned results must have the record: 当我按如下方式键入输入字符串(仅是粗体字符串)时,返回的结果必须具有记录:
**n** or
**n k** or
**n p** or
**k p** or
**n k p**
I use SQLite, and at the currrent I do not have any idea. 我使用SQLite,目前我还没有任何想法。 Could you give me a suggestion about this?
你能给我一个建议吗?
You could use wildcard %
character to match any string/character and the LIKE
statement as well. 您可以使用通配符
%
字符来匹配任何字符串/字符以及LIKE
语句。
For first name that starts with 'N' for example would be: 例如,以“ N”开头的名字是:
SELECT * FROM yourTable WHERE FirstName LIKE 'N%'
For first and last name it would be: 对于名字和姓氏,它将是:
SELECT * FROM yourTable WHERE FirstName LIKE 'N%' or LastName LIKE 'P%'
Update : 更新 :
And if you are not sure if the input is for first name, middle name or last name then you can do it on multiple fields, like: 而且,如果您不确定输入的是名字,中间名还是姓氏,则可以在多个字段中进行输入,例如:
SELECT * FROM yourTable
WHERE FirstName LIKE 'N%' or MiddleName LIKE 'N%' or LastName LIKE 'N%'
And if there are more than one letter (say two) it would get a little crazier like: 并且如果有多个字母(例如两个字母),它将变得有点疯狂:
SELECT * FROM yourTable
WHERE FirstName LIKE 'N%' or MiddleName LIKE 'N%' or LastName LIKE 'N%'
or FirstName LIKE 'P%' or MiddleName LIKE 'P%' or LastName LIKE 'P%'
But using the solution above would not be good. 但是使用上面的解决方案将不是很好。 So, it would be good that you allow the users to either:
因此,最好允许用户执行以下任一操作:
For option number 1 you query would look like this: 对于选项1,您查询的内容如下所示:
SELECT * FROM yourTable
WHERE FirstName LIKE 'N%' or MiddleName LIKE 'K%' or LastName LIKE 'P%'
For option number 2 you query would look like this: 对于选项2,您查询的内容如下所示:
SELECT * FROM yourTable
WHERE FirstName LIKE 'N%' or MiddleName LIKE 'N%' or LastName LIKE 'N%'
or if it a String it would probably look like this: 或者,如果它是字符串,则可能看起来像这样:
SELECT * FROM yourTable
WHERE FirstName LIKE 'Ngu%' or MiddleName LIKE 'Ngu%' or LastName LIKE 'Ngu%'
In other words take control of how the users input and give him/her little room for potential error not to mention confusion. 换句话说,控制用户的输入方式,并为他/她留出一点可能的错误的空间,更不用说困惑了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.