简体   繁体   English

如何查询每个字符的首字母?

[英]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: 因此,最好允许用户执行以下任一操作:

  1. Input letters for each name, that is, a letter for lastname and firstname and middlename if needed 输入每个名称的字母,即根据需要输入姓氏,名字和中间名的字母
  2. Just input one letter (or of course a String) then search either from firstname, lastname and middlename. 只需输入一个字母(或者当然是一个字符串),然后从名字,姓氏和中间名中搜索即可。

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.

相关问题 如何检查字符串的第一个字符,如果是字母,C# 中的任何字母 - How to check first character of a string if a letter, any letter in C# 如何进行 T-SQL 查询,其中输出的第一个字母等于一个字符 - How to make a T-SQL query where the first letter of the output equals to a character 如何把每个句子的首字母大写? - How to capitalize first letter of each sentence? 查找字符串中第一个是字母的字符 - Find the first character in a string that is a letter 正则表达式每个单词的首字母大写由空格和 3 到 29 个字符长...C# - Regex first letter of each word Upper Case separated by spaces and 3 to 29 character long…C# 如何替换字符串中每个单词的第一个字母? - How do i replace first letter in each word in a string? 如何删除文本框的首字母(每行) - How to delete first letter of text box(for each line) 如何将每个句子的首字母大写,并给用户将字母更改为较低的机会? - How to capitalize first letter of each sentence AND give User the chance to change the letter to lower? 如何从 streamreader 读取第一行的第一个字符? (例如 txt 文件中的字母 H - How can I read from streamreader the first character from the first line? ( Example the Letter H that is in the txt file 如何在NHibernate中查询每个组中的第一个条目 - How to query the first entry in each group in NHibernate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM