2015-04-24 69 views
1

我有一個MySQL表中的同一表的兩個欄查詢如下如何從那裏等於一個值

id| reagent1 | reagent 2 
1 | coffee | milk 
2 | sugar  | coffee 
3 | coffee | salt 

如果我想獲得的咖啡試劑,結果應該是

reagentofcoffee 
milk 
sugar 
salt 

如何查詢以獲得預期的結果?我是mysql新手,我嘗試了很多次。

回答

2

您可以使用case

select case when reagent1 = 'coffee' then reagent2 
      when reagent2 = 'coffee' then reagent1 end as reagentofcoffee 
from tbl 
0

select名單A case表達應該做的伎倆:

SELECT CASE reagent1 WHEN 'coffee' THEN reagent2 ELSE reagent1 END 
      AS reagentofcoffee 
FROM reagents 
WHERE 'coffee' IN (reagent1, reagent2) 
1
SELECT IF(reagent1 = 'coffee',reagent1,reagent2) as reagent FROM table WHERE reagent1 = 'coffee' OR reagent2 = 'coffee' 
相關問題