开发者

Select ids based on conditions on multiple rows

开发者 https://www.devze.com 2023-02-26 02:01 出处:网络
I have a table that looks like this Tag_IdBidCategoryTitle 11ToysLudo 21ToysSnakes 32FoodPotato 41FoodChicken

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号