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

如何在mysql数据库中选择多个具有多个条件的值,mysql,vb.net,Mysql,Vb.net,这是我的示例表单,我想实现的是,我在任何字段中编写的任何内容,当我单击“搜索”时,它将过滤所有数据。例如,当我搜索发布日期“2015/11/26”并有“单位”描述时,所有详细信息都将显示在表中,但正如您所看到的,即使是“2015/11/27”详细信息也会显示,因为它有“单位”描述。我知道原因是因为我使用或作为操作员,但如果我使用和,它将不显示任何内容,因为其他文本框(如PO编号等)为空 有人能帮我吗? 提前谢谢 这是我的查询示例查询 SELECT * FROM tbl_somonitoring

这是我的示例表单,我想实现的是,我在任何字段中编写的任何内容,当我单击“搜索”时,它将过滤所有数据。例如,当我搜索发布日期“2015/11/26”并有“单位”描述时,所有详细信息都将显示在表中,但正如您所看到的,即使是“2015/11/27”详细信息也会显示,因为它有“单位”描述。我知道原因是因为我使用或作为操作员,但如果我使用和,它将不显示任何内容,因为其他文本框(如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 & "'

也许你可以试试这个

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)
Dim commandString作为字符串
如果postdate.Text Nothing或ponumber.Text Nothing或docnumber.Text Nothing或TextBox1.Text Nothing或tagging.Text Nothing或accntname.Text Nothing或address.Text Nothing或description.Text Nothing或modelname.Text Nothing或requestqty.Text Nothing或colAND.Text Nothing或amount.Text Nothing然后
cmdstring=cmdstring&“其中”
Dim FirstState为整数=0
如果postdate.text无任何内容,则
cmdstring=cmdstring&“post_date=“&posteddate.Text
第一状态+=1
如果结束
如果ponumber.Text无任何内容,则
如果FirstState>0,则cmdstring=cmdstring&“和”
cmdstring=cmdstring&“po_num=“&ponumber.Text”
第一状态+=1
如果结束
如果docnumber.Text无任何内容,则
如果FirstState>0,则cmdstring=cmdstring&“和”
cmdstring=cmdstring&“doc_num=“&docnumber.Text”
第一状态+=1
如果结束
如果TextBox1.Text Nothing,则
如果FirstState>0,则cmdstring=cmdstring&“和”
cmdstring=cmdstring&“so_num=“&TextBox1.Text
第一状态+=1
如果结束
如果标记。文本无任何内容,则
如果FirstState>0,则cmdstring=cmdstring&“和”
cmdstring=cmdstring&“tagging=“&tagging.Text
如果结束
如果accntname.Text无任何内容,则
如果FirstState>0,则cmdstring=cmdstring&“和”
cmdstring=cmdstring&“accnt\u name=“&accntname.Text”
第一状态+=1
如果结束
如果地址。文本无任何内容,则
如果FirstState>0,则cmdstring=cmdstring&“和”
cmdstring=cmdstring&“address=“&address.Text”
第一状态+=1
如果结束
如果描述。文本无任何内容,则
如果FirstState>0,则cmdstring=cmdstring&“和”
cmdstring=cmdstring&“description=“&description.Text”
第一状态+=1
如果结束
如果modelname.Text为空,则
如果FirstState>0,则cmdstring=cmdstring&“和”
cmdstring=cmdstring&“model_name=“&modelname.Text”
第一状态+=1
如果结束
如果请求数量文本为空,则
如果FirstState>0,则cmdstring=cmdstring&“和”
cmdstring=cmdstring&“请求数量=&requestqty.Text”
第一状态+=1
如果结束
如果colAND.Text没有任何内容,则
如果FirstState>0,则cmdstring=cmdstring&“和”
cmdstring=cmdstring&“colAND=”&colAND.Text
第一状态+=1
如果结束
如果是amount.Text,则无任何内容
如果FirstState>0,则cmdstring=cmdstring&“和”
cmdstring=cmdstring&“amount=“&amount.Text”
第一状态+=1
如果结束
如果结束
dim cmd as MySqlCommand=新的MySqlCommand(cmdstring,con)

这样,您就可以检查每个字段并使用适当的搜索查询,您需要做的是如下测试:

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

我认为您需要在where条件下使用
,而不是
。按照你的要求。如果您只需要
PostDate
Description
相关条件,那么为什么要在where子句中比较其他值??尝试以下操作:-@Vishal是的,我已经尝试过使用,但是由于其他字段为空,它没有显示任何内容。我怀疑您的查询计划或您只是对实际需求感到困惑,如果可能,请提供示例数据以及表DDLim对不起,我想做的是,无论我在任何字段中输入什么,它都会过滤所有数据,这就是为什么我会将其与所有字段进行比较。