[英]C# SQL Server Query First/Last Name Columns where First Name Has Space
我有一個像這樣的表結構:
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所示的代碼無效。
如何優雅地升級代碼或查詢以處理這種情況? 謝謝。
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] + '%'
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.