如何在具有多个条件的mysql数据库中选择多个值

时间:2015-11-27 07:18:33

标签: mysql vb.net

sample form

这是我的示例表单,我想要发生的是我在任何字段中写的任何内容,当我点击搜索它将过滤所有数据。例如,当我搜索" 2015/11/26"的发布日期时并且描述了" UNIT"所有细节都将显示在表格中,但正如您所看到的,即使是" 2015/11/27"还会显示详细信息,因为它有" UNIT"的描述。我知道原因是因为我使用OR作为运算符,但是如果我使用AND,它什么都不显示,因为像PO号等其他文本框是空的。

有人能帮帮我吗? 提前谢谢!

这是我的查询示例查询

SELECT * FROM tbl_somonitoring 
where post_date ='" & posteddate.Text & "' 
or po_num ='" & ponumber.Text & "' 
or doc_num='" & docnumber.Text & "' 
or so_num='" & TextBox1.Text & "' 
or tagging='" & tagging.Text & "' 
or accnt_name='" & accntname.Text & "' 
or address='" & address.Text & "' 
or description='" & description.Text & "' 
or model_name='" & modelname.Text & "' 
or request_qty='" & requestqty.Text & "' 
or color='" & color.Text & "' 
or amount='" & amount.Text & "'

2 个答案:

答案 0 :(得分:0)

也许你可以试试这个。

Dim commandString as String

if postdate.Text <> Nothing or ponumber.Text <> Nothing or docnumber.Text <> Nothing or TextBox1.Text <> Nothing or tagging.Text <> Nothing or accntname.Text <> Nothing or address.Text <> Nothing or description.Text <> Nothing or modelname.Text <> Nothing or requestqty.Text <> Nothing or colAND.Text <> Nothing or amount.Text <> Nothing Then

cmdstring = cmdstring & "Where "

Dim FirstState as Integer = 0

If postdate.text <> Nothing Then

    cmdstring = cmdstring & "post_date = " & posteddate.Text
    FirstState+=1


End If

If ponumber.Text <> Nothing Then

    If FirstState > 0 Then cmdstring = cmdstring & " AND "
    cmdstring = cmdstring & "po_num = " & ponumber.Text
    FirstState+=1

End If

If docnumber.Text <> Nothing  Then

    If FirstState > 0 Then cmdstring = cmdstring & " AND "
    cmdstring = cmdstring & "doc_num = " & docnumber.Text 
     FirstState+=1

End If

If TextBox1.Text <> Nothing Then

    If FirstState > 0 Then cmdstring = cmdstring & " AND "
    cmdstring = cmdstring & " so_num = " & TextBox1.Text
     FirstState+=1

End If

If tagging.Text <> Nothing Then

    If FirstState > 0 Then cmdstring = cmdstring & " AND "
    cmdstring = cmdstring & " tagging = " & tagging.Text

End If

If accntname.Text <> Nothing  Then

    If FirstState > 0 Then cmdstring = cmdstring & " AND "
    cmdstring = cmdstring & "accnt_name = " & accntname.Text
     FirstState+=1

End If

If address.Text <> Nothing Then

    If FirstState > 0 Then cmdstring = cmdstring & " AND "
    cmdstring = cmdstring & "address = " & address.Text
     FirstState+=1

End If

If description.Text <> Nothing Then

    If FirstState > 0 Then cmdstring = cmdstring & " AND "
    cmdstring = cmdstring & "description = " & description.Text
     FirstState+=1

End If

If modelname.Text <> Nothing Then

    If FirstState > 0 Then cmdstring = cmdstring & " AND "
    cmdstring = cmdstring & " model_name = " & modelname.Text
     FirstState+=1

End If

If requestqty.Text <> Nothing Then

    If FirstState > 0 Then cmdstring = cmdstring & " AND "
    cmdstring = cmdstring & "request_qty = " & requestqty.Text
     FirstState+=1

End If

If colAND.Text <> Nothing Then

    If FirstState > 0 Then cmdstring = cmdstring & " AND "
    cmdstring = cmdstring & "colAND = " & colAND.Text
     FirstState+=1

End If

If amount.Text <> Nothing Then

    If FirstState > 0 Then cmdstring = cmdstring & " AND "
    cmdstring = cmdstring & " amount = " & amount.Text 
     FirstState+=1

End If

End If

dim cmd as MySqlCommand = new MySqlCommand(cmdstring,con)

您可以检查每个字段并使用正确的搜索查询

答案 1 :(得分:0)

你需要做的就是这样测试:

(field1 = val1 or field1 = "")
And
(field2 = val2 or field2 = "")
And
...