目前我正在使用以下代码搜索整个表格中的单词
Dim searchKey As String = "Sample"
Dim mysql As String = "SELECT * FROM myTable " & _
"WHERE col1 LIKE '%" & searchKey & "%' " & _
" OR col2 LIKE '%" & searchKey & "%' " & _
" OR col3 LIKE '%" & searchKey & "%' " & _
" OR col4 LIKE '%" & searchKey & "%' "
但当列数增加时,查询变得更加困难。所以任何人都可以建议我做一些替代方法。
答案 0 :(得分:0)
使用循环以编程方式构建String。在此示例中,您只需要将列添加到数组中。
Dim searchKey As String = "Sample"
Dim array() As String = {"col1", "col2", "col3", "col4"}
Dim b As Boolean = True
Dim mysql As String = "SELECT * FROM myTable WHERE "
For Each str As String In array
If b Then
mysql = mysql & str + " LIKE '%" & searchKey & "%'"
Else
mysql = mysql & " OR " & str & " LIKE '%" & searchKey & "%'"
End If
b = False
Next
Debug.WriteLine(mysql)
输出>>
SELECT * FROM myTable WHERE col1 LIKE '%Sample%' OR col2 LIKE '%Sample%' OR col3 LIKE '%Sample%' OR col4 LIKE '%Sample%'
答案 1 :(得分:0)
您还可以获取MySQL提供的全文搜索帮助。 此链接提供有关全文搜索的详细信息,http://dev.mysql.com/doc/refman/5.0/en/fulltext-natural-language.html
答案 2 :(得分:0)
您可以这样做,如果表格结构发生变化,您需要在查询中添加/删除列。
SELECT * FROM myTable WHERE Concat(col1, '',col2, '', coln) LIKE '%searchKey%'