首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在mysql中将ot_total和ot_shipping放在一行中?

如何在mysql中将ot_total和ot_shipping放在一行中?
EN

Stack Overflow用户
提问于 2013-08-26 06:47:34
回答 2查看 38关注 0票数 0

如何在mysql中将ot_total和ot_shipping放在一行中?

我想从我们的桌子上得到每一份订单的费用。我怎样才能在mysql中得到这个表?

非常感谢。

代码语言:javascript
复制
SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `ot`
-- ----------------------------
DROP TABLE IF EXISTS `ot`;
CREATE TABLE `ot` (
  `id` int(10) NOT NULL auto_increment,
  `orders_id` int(10) default NULL,
  `class` varchar(30) default NULL,
  `value` float(30,0) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of ot
-- ----------------------------
INSERT INTO `ot` VALUES ('1', '1', 'ot_shipping', '10');
INSERT INTO `ot` VALUES ('2', '1', 'ot_total', '100');
INSERT INTO `ot` VALUES ('3', '1', 'ot_insurance', '1');
INSERT INTO `ot` VALUES ('4', '2', 'ot_shipping', '10');
INSERT INTO `ot` VALUES ('5', '2', 'ot_total', '80');

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-08-26 06:50:56

代码语言:javascript
复制
select orders_id, 
       sum(case when class='ot_shipping' then value else 0 end) as shipping,
       sum(case when class='ot_total' then value else 0 end) as total
from ot
GROUP BY orders_id;

SqlFiddle

票数 1
EN

Stack Overflow用户

发布于 2013-08-26 06:54:49

代码语言:javascript
复制
select o.orders_id, 
       sum(case when o.class='ot_shipping' then o.value else 0 end) as ot_shipping,
       sum(case when o.class='ot_total' then o.value else 0 end) as ot_total
from ot o
GROUP BY o.orders_id;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18438180

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档