0

我有一个像这样的名为 my_table 的表

+------------+--------+--------+
|  product   | type_1 | type_2 |
+------------+--------+--------+
| Banana     | A1     | B1     |
| Banana     | B1     | B2     |
| Watermelon | A      | B3     |
| Orange     | B      | B4     |
+------------+--------+--------+

所以我想查询产品香蕉,然后它返回type_2,另一个产品返回type_1

所以预期的结果就是这样

+------------+------+
|  product   | type |
+------------+------+
| Banana     | B1   |  
| Banana     | B2   |  
| Watermelon | A    |  
| Orange     | B    | 
+------------+------+
4

1 回答 1

1

您可以使用 CASE 选择所需的列。

SELECT product, (CASE produt
                     WHEN 'Banana' THEN type_2
                     ELSE type_1
                 END) as type

FROM my_table                           
于 2020-04-17T10:42:36.397 回答