按名称搜索的SQL查询(包含两个或多个单词的组合)

时间:2022-11-05 09:31:13

When i search by using

当我用

....LIKE '%" + txtName.Text + "%'.....

Then it returns all the rows containing txtName. It is okay for name like

然后返回所有包含txtName的行。名字是可以的

  • Bijay or
  • Bijay或
  • Bijay kumar.

    Bijay库马尔。

    And rows contains data like

    行包含类似的数据

  • Bijay,

    Bijay,

  • Bijay kumar kush,
  • Bijay kumar库什
  • Bijay kush.
  • Bijay库什。

In this case it should return, Bijay kumar kush and Bijay Kush when search by using txtName like Bijay Kush. But not getting the output like this. It is empty when search by using first name and last name without supplying middle name.HeretxtNameis the Full Name text Box used for searching the name.I want to know where am I making the mistake.Any help would be appreciated. thanks in advance.I am using this type of query which doesn't return the result if searched without middle name as stated above.

在这种情况下,它应该返回,Bijay kumar kush和Bijay kush在搜索时使用了txtName,如Bijay kush。但是不能得到这样的输出。使用姓名和姓搜索时,它是空的,不提供中间名。HeretxtNameis用于搜索名称的全名文本框。我想知道我在哪里犯了错误。如有任何帮助,我们将不胜感激。提前谢谢。我正在使用这种类型的查询,如果没有如上所述的中间名进行搜索,它不会返回结果。

...
if (!String.IsNullOrEmpty(txtName.Text) && !String.IsNullOrWhiteSpace(txtName.Text))
       SearchSqlDataSource.SelectCommand += " AND (New_Employees.Full_Name LIKE '%" + txtName.Text + "%' OR New_Employees.Full_Name LIKE '%" + txtName.Text + "%')

1 个解决方案

#1


2  

I might suggest replacing all spaces with the wildcard character. You can do this in the application or the database. Assuming the underlying database is SQL Server, here is the database code:

我可能建议用通配符替换所有空格。您可以在应用程序或数据库中执行此操作。假设底层数据库是SQL Server,下面是数据库代码:

WHERE New_Employees.Full_Name LIKE REPLACE('%" + txtName.Text + "%', ' ', '%')

#1


2  

I might suggest replacing all spaces with the wildcard character. You can do this in the application or the database. Assuming the underlying database is SQL Server, here is the database code:

我可能建议用通配符替换所有空格。您可以在应用程序或数据库中执行此操作。假设底层数据库是SQL Server,下面是数据库代码:

WHERE New_Employees.Full_Name LIKE REPLACE('%" + txtName.Text + "%', ' ', '%')