sql查询如何把2“where”条件

时间:2012-01-19 04:58:55

标签: sql vb.net

enter image description here

首先这是表关系。 以下是我想要实现的目标

Dim ad As New SqlDataAdapter("SELECT inventory.ItemName,record_item.Amount FROM record_item,inventory WHERE (record_item.RecordID = '" & listbox1.SelectedItem & "')", conn)

如果我试图根据我拥有的RecordID获取ItemName,我是否需要放2条件?如果我这样做,怎么样?

WHERE (record_item.RecordID = '" & listbox1.SelectedItem & "')WHERE(record_item.ItemID=inventory.ItemID)

7 个答案:

答案 0 :(得分:3)

WHERE (something) AND (something else)

答案 1 :(得分:2)

只是这样做:


SELECT inventory.ItemName,record_item.Amount FROM record_item,inventory
WHERE record_item.RecordID = '" & listbox1.SelectedItem & "' and record_item.ItemID=inventory.ItemID

答案 2 :(得分:2)

没有第二个WHERE子句

WHERE 
    (record_item.RecordID = '" & listbox1.SelectedItem & "') 
and (record_item.ItemID=inventory.ItemID)

答案 3 :(得分:2)

而不是像你这样使用两个条件

WHERE(第一条件)AND(第二条件)。

答案 4 :(得分:0)

  

Dim ad As New SqlDataAdapter(“SELECT inventory.ItemName,record_item.Amount   FROM record_item,inventory WHERE record_item.RecordID ='“& listbox1.SelectedItem&”'AND record_item.ItemID = inventory.ItemID“,conn)

答案 5 :(得分:0)

检查以下代码......

Dim ad As New SqlDataAdapter("SELECT inventory.ItemName, record_item.Amount FROM record_item, inventory WHERE (record_item.RecordID = '" & listbox1.SelectedItem & "' AND record_item.ItemID=inventory.ItemID)", conn)

如果您的record_item.RecordID是整数,那么下面的查询就可以了......

Dim ad As New SqlDataAdapter("SELECT inventory.ItemName, record_item.Amount FROM record_item, inventory WHERE (record_item.RecordID = " & listbox1.SelectedItem & " AND record_item.ItemID=inventory.ItemID)", conn)

答案 6 :(得分:0)

在简单的SELECT查询中不能使用多个WHERE,您可以使用SUBQUERY或其他。

在您的情况下,为什么您没有实施建议的答案。 你也可以使用IN子句,


WHERE (record_item.RecordID IN ('" & listbox1.SelectedItem & "', inventory.ItemID )