2011-04-14 157 views
1

我有一個表,看起來像這樣根據條件對多行

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 

我想寫基於以下條件其中發現投標查詢選擇IDS。

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

可以b'MySQL的case'聲明幫助ü :http://dev.mysql.com/doc/refman/5.0/en/case-statement.html – diEcho 2011-04-14 06:58:06

+0

您能描述上述示例數據所需的預期輸出嗎? – Dheer 2011-04-14 07:25:39

+0

預期的輸出是競標1 – Ankit 2011-04-14 07:38:43

回答

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")) 

你已經寫出了最困難的。

+0

這不會在多行中找到出價。關係劃分問題的種類 – Ankit 2011-04-14 07:16:28

+1

所以,我想,我不明白你想要什麼請求返回 – Elorfin 2011-04-14 07:29:59

0

嘗試

SELECT Bid FROM bidTable WHERE Category = "Toys" 
UNION 
SELECT Bid FROM bidTable WHERE Category = "Food" 
UNION 
SELECT Bid FROM bidTable WHERE Category = "Name" 

我認爲ü得到問題出那麼爲什麼這麼多condition..btw u能在WHERE子句添加條件的所有行