use*_*755 2 mysql sql select where-clause
我有一个零售商店的数据,下面的列
col_1 qty_1 col_2 qty_2 col_3 qty_3
Green 5 Red 8 Yellow 10
Run Code Online (Sandbox Code Playgroud)
如果我想知道绿色的数量,那么我可以编写一个简单的查询,其中col_1 ='Green'。
但是我没有在col_1中得到绿色的颜色代码。它将在col_1,col_2和col_3之间不断改组,如下所示
col_1 qty_1 col_2 qty_2 col_3 qty_3
Green 5 Red 8 Yellow 10
Yellow 10 Green 7 Red 20
Run Code Online (Sandbox Code Playgroud)
我怎样才能有一个查询来提取可用的绿色数量而不每次都更改查询?
select case when col_1 = 'Green' then qty_1
when col_2 = 'Green' then qty_2
when col_3 = 'Green' then qty_3
end as qty
from your_table
where 'Green' in (col_1, col_2, col_3)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
51 次 |
| 最近记录: |