Select ids based on conditions on multiple rows
I have a table that looks like this
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
I want to write a query which finds Bid based on the following condition.
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")
)
Notice that there are ORs between same categories and ANDs between different categories. Is there an开发者_Go百科y way to achieve this with single query?
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") )
You have already written the most difficult.
try with
SELECT Bid FROM bidTable WHERE Category = "Toys"
UNION
SELECT Bid FROM bidTable WHERE Category = "Food"
UNION
SELECT Bid FROM bidTable WHERE Category = "Name"
i think u get all the rows shown in question then why so much condition..btw u can add condition in WHERE
clause
精彩评论