我有一张看起来像这样的表
Tag_Id Bid Category Title
1 1 Toys Ludo
2 1 Toys Snakes
3 2 Food Potato
4 1 Food Chicken
5 1 Name Chris
6 2 Name Joe
我想根据以下条件编写一个查找Bid的查询。
Find Bid's where
(
(Category = "Toys" AND Title = "Ludo")
OR
(Category = "Toys" AND Title = "Snakes")
)
AND (Category = "Food" AND Title = "Potato")
AND
(
(Category = "Name" AND Title = "Chris")
OR
(Category = "Name" AND Title = "Joe")
)
请注意,不同类别之间的相同类别和AND之间存在OR。有没有办法通过单一查询实现这一目标?
答案 0 :(得分:1)
SELECT Bid
FROM yourTable
WHERE ( (Category = "Toys" AND Title = "Ludo") OR (Category = "Toys" AND Title = "Snakes") )
AND (Category = "Food" AND Title = "Potato")
AND ( (Category = "Name" AND Title = "Chris") OR (Category = "Name" AND Title = "Joe") )
你已经写过最难的了。
答案 1 :(得分:0)
尝试
SELECT Bid FROM bidTable WHERE Category = "Toys"
UNION
SELECT Bid FROM bidTable WHERE Category = "Food"
UNION
SELECT Bid FROM bidTable WHERE Category = "Name"
我认为你得到了所有显示的行然后为什么这么多条件......你可以在WHERE
子句中添加条件