我有一个名为my_table的表,就像这个一样
+------------+--------+--------+
| product | type_1 | type_2 |
+------------+--------+--------+
| Banana | A1 | B1 |
| Banana | B1 | B2 |
| Watermelon | A | B3 |
| Orange | B | B4 |
+------------+--------+--------+
所以我想查询产品banana,然后它返回type_2,另一个产品返回type_1
所以预期的结果就像这个
+------------+------+
| product | type |
+------------+------+
| Banana | B1 |
| Banana | B2 |
| Watermelon | A |
| Orange | B |
+------------+------+
您可以使用CASE来选择所需的列。
SELECT product, (CASE produt
WHEN 'Banana' THEN type_2
ELSE type_1
END) as type
FROM my_table