[英]How to query from two column of a same table where equal to one value
I have a mysql table as follow 我有一个mysql表如下
id| reagent1 | reagent 2
1 | coffee | milk
2 | sugar | coffee
3 | coffee | salt
If I would like to get the reagents of coffee, the result should be 如果我想得到咖啡的试剂,结果应该是
reagentofcoffee
milk
sugar
salt
How can I make a query to get expected result? 如何进行查询以获得预期结果? I am new to mysql and I have tried many time. 我是mysql的新手,我已经尝试了很多次。
You can use case
: 你可以使用case
:
select case when reagent1 = 'coffee' then reagent2
when reagent2 = 'coffee' then reagent1 end as reagentofcoffee
from tbl
SELECT IF(reagent1 = 'coffee',reagent1,reagent2) as reagent FROM table WHERE reagent1 = 'coffee' OR reagent2 = 'coffee'
A case
expression in the select
list should do the trick: select
列表中的case
表达式应该可以解决问题:
SELECT CASE reagent1 WHEN 'coffee' THEN reagent2 ELSE reagent1 END
AS reagentofcoffee
FROM reagents
WHERE 'coffee' IN (reagent1, reagent2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.