您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)怎么在mySql中統(tǒng)計(jì)數(shù)量,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
select project_no, sum(case when device_state=0 then 1 else 0 end)as offTotal , sum(case when device_state=1 then 1 else 0 end)as onlineTotal, sum(1)total from iot_d_device group by project_no order by project_no
補(bǔ)充:MySQL一條SQL語(yǔ)句查詢(xún)多條統(tǒng)計(jì)結(jié)果
商城項(xiàng)目難免會(huì)遇到用戶(hù)個(gè)人中心頁(yè)查詢(xún)不同狀態(tài)訂單數(shù)量的問(wèn)題。當(dāng)然這個(gè)問(wèn)題并不難,可以寫(xiě)一個(gè)DAO層方法,以狀態(tài)作為入?yún)ⅲ看蝹魅氩煌瑺顟B(tài)值依次查詢(xún)相應(yīng)狀態(tài)的訂單數(shù)量。
今天在寫(xiě)H5端接口時(shí),我想換種方式查,也就是通過(guò)一條SQL查詢(xún)出多個(gè)狀態(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 '訂單號(hào)', `user_id` int(11) DEFAULT NULL COMMENT '用戶(hù)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)建時(shí)間', 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');
核心SQL語(yǔ)句如下:
SELECT COUNT(CASE order_status WHEN 1 THEN 1 END) AS "狀態(tài)1",COUNT(CASE order_status WHEN 2 THEN 1 END) AS "狀態(tài)2",COUNT(CASE order_status WHEN 3 THEN 1 END) AS "狀態(tài)3",COUNT(CASE order_status WHEN 4 THEN 1 END) AS "狀態(tài)4" FROM `mini_test_order`;
或如下:
SELECT COUNT(CASE WHEN order_status = 1 THEN 1 END) AS "狀態(tài)1",COUNT(CASE WHEN order_status = 2 THEN 1 END) AS "狀態(tài)2",COUNT(CASE WHEN order_status = 3 THEN 1 END) AS "狀態(tài)3",COUNT(CASE WHEN order_status = 4 THEN 1 END) AS "狀態(tài)4" FROM `mini_test_order` ;
當(dāng)然,SQL語(yǔ)句不僅僅局限于上述兩種寫(xiě)法,喜歡探究的童靴歡迎留言補(bǔ)充。
1.簡(jiǎn)單函數(shù)
CASE [col_name] WHEN [value1] THEN [result1]…ELSE [default] END
2.搜索函數(shù)
CASE WHEN [expr] THEN [result1]…ELSE [default] END
前者枚舉col_name這個(gè)字段值為符合條件value1時(shí)所有可能的值;
后者可以寫(xiě)判斷,并且搜索函數(shù)只會(huì)返回第一個(gè)符合條件的值,其他case被忽略。
關(guān)于怎么在mySql中統(tǒng)計(jì)數(shù)量就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。