如何根据几个条件选择数据 mysql
how to selected data based on several conditions mysql
我有一个叫 my_table 的 table 像这样
+------------+--------+--------+
| product | type_1 | type_2 |
+------------+--------+--------+
| Banana | A1 | B1 |
| Banana | B1 | B2 |
| Watermelon | A | B3 |
| Orange | B | B4 |
+------------+--------+--------+
所以我想查询 IF 产品香蕉,然后它 returns type_2,另一个产品 return type_1
所以预期的结果就是这样
+------------+------+
| product | type |
+------------+------+
| Banana | B1 |
| Banana | B2 |
| Watermelon | A |
| Orange | B |
+------------+------+
您可以使用 CASE 来 select 您想要的列。
SELECT product, (CASE produt
WHEN 'Banana' THEN type_2
ELSE type_1
END) as type
FROM my_table
我有一个叫 my_table 的 table 像这样
+------------+--------+--------+
| product | type_1 | type_2 |
+------------+--------+--------+
| Banana | A1 | B1 |
| Banana | B1 | B2 |
| Watermelon | A | B3 |
| Orange | B | B4 |
+------------+--------+--------+
所以我想查询 IF 产品香蕉,然后它 returns type_2,另一个产品 return type_1
所以预期的结果就是这样
+------------+------+
| product | type |
+------------+------+
| Banana | B1 |
| Banana | B2 |
| Watermelon | A |
| Orange | B |
+------------+------+
您可以使用 CASE 来 select 您想要的列。
SELECT product, (CASE produt
WHEN 'Banana' THEN type_2
ELSE type_1
END) as type
FROM my_table