我有一个像这样的表结构:
user_id | first_name | last_name
1 | John | Smith
2 | Mary | Jones
3 | Sang He | Lau
// here is a sample of the code I currently have to do a user search
public List<string> getResults(string autocompleteQuery) {
string[] tokens = autocompleteQuery.Split(' ');
// token[0] = first name parameter (@firstName)
// token[1] = last name parameter (@lastName)
// code here executes the following query:
// SELECT * FROM [user] WHERE first_name = @firstName AND last_name = @lastName
}
我的问题是,当user_id = 3中显示第一个名称中有空格时,以下使用空格作为列表分隔符并与查询结合使用的代码不起作用。
如何优雅地升级代码或查询来处理这种情况?感谢。
答案 0 :(得分:1)
SELECT * FROM [user]
WHERE
first_name + last_name LIKE '%' + token[0] + '%'
AND
first_name + last_name LIKE '%' + token[1] + '%'
AND
first_name + last_name LIKE '%' + token[2] + '%'
...