商城項目難免會遇到用戶個人中心頁查詢不同狀態(tài)訂單數(shù)量的問題。當然這個問題并不難,可以寫一個DAO層方法,以狀態(tài)作為入?yún)?,每次傳入不同狀態(tài)值依次查詢相應(yīng)狀態(tài)的訂單數(shù)量。
今天在寫H5端接口時,我想換種方式查,也就是通過一條SQL查詢出多個狀態(tài)的訂單數(shù)量。在網(wǎng)上搜了搜,方法可行,所以就嘗試了下,果不其然成功了。
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for mini_test_order
-- ----------------------------
DROP TABLE IF EXISTS `mini_test_order`;
CREATE TABLE `mini_test_order` (
`id` int(11) NOT NULL,
`order_no` varchar(32) DEFAULT NULL COMMENT '訂單號',
`user_id` int(11) DEFAULT NULL COMMENT '用戶id',
`shop_id` int(11) DEFAULT NULL COMMENT '商家id',
`order_status` tinyint(1) DEFAULT NULL COMMENT '訂單狀態(tài)',
`create_time` int(10) DEFAULT NULL COMMENT '創(chuàng)建時間',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of mini_test_order
-- ----------------------------
INSERT INTO `mini_test_order` VALUES ('1', 'aaaaaaaaa', '11', '111', '1', '1573041313');
INSERT INTO `mini_test_order` VALUES ('2', 'bbbbbbbb', '11', '222', '1', '1573041313');
INSERT INTO `mini_test_order` VALUES ('3', 'cccccccccc', '11', '333', '2', '1573041313');
INSERT INTO `mini_test_order` VALUES ('4', 'dddddddd', '11', '222', '3', '1573041313');
INSERT INTO `mini_test_order` VALUES ('5', 'eeeeeeeee', '11', '111', '4', '1573041313');
INSERT INTO `mini_test_order` VALUES ('6', 'ffffffffffffff', '11', '111', '3', '1573041313');
INSERT INTO `mini_test_order` VALUES ('7', 'gggggggg', '11', '222', '4', '1573041313');
INSERT INTO `mini_test_order` VALUES ('8', 'hhhhhhhhh', '11', '111', '4', '1573041313');
INSERT INTO `mini_test_order` VALUES ('9', 'iiiiiiiiiiiiiiiiiii', '11', '333', '3', '1573041313');
INSERT INTO `mini_test_order` VALUES ('10', 'jjjjjjjjjjjjjjjjjj', '11', '222', '1', '1573041313');