简体   繁体   English

MySQL-如何从一列中查询具有不同值的不同列

[英]Mysql - how to query different column with different values from one column

I tried to create different column with different values from one column... I still can't figure out how to handle this request..I have table like this 我试图从一个列创建具有不同值的不同列...我仍然不知道如何处理此请求。

No  Description   Delivery  unit
1   Shipment to A   Car     2
2   Shipment to B   plane   4
3   Shipment to C   Ship    3
4   Shipment to A   Car     1
5   Shipment to C   Ship    2

I want to create something like this 我想创建这样的东西

no  description        Car   Plane  Ship
1   shipment to all     3      4     5

how to achieve that... 如何实现...

thank in advance. 预先感谢。

You can use conditional aggregation: 您可以使用条件聚合:

select 1 as no, 'Shipment to all' as description,
       sum(case when delivery = 'Car' then unit else 0 end) as car,
       sum(case when delivery = 'plane' then unit else 0 end) as plane,
       sum(case when delivery = 'ship' then unit else 0 end) as ship
from t;

It is not clear how the first two columns are calculated, so I just included constants. 目前尚不清楚前两列的计算方式,因此我只包含了常量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM