SELECT TrueName,SUM(Money) as Money,DATE_FORMAT(fin_date,"%Y-%m") as dateMonth FROM tbl_fin WHERE TrueName = "小明" GROUP BY CONCAT(TrueName,DATE_FORMAT(fin_date,"%Y-%m")) ORDER BY dateMonth ASC
SUM(Money) as Money, 的意思是統計(加)Money的總金額,
DATE_FORMAT(fin_date,"%Y-%m") as dateMonth ,把日期格式化為年月的形式
GROUP BY CONCAT(TrueName,DATE_FORMAT(fin_date,"%Y-%m")) 這裡需要拆開來理解,
其實這個是個很基礎的知識。
如下示例:
DDL:
CREATE TABLE `tbl_fin` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `fin_date` date DEFAULT NULL, `TrueName` varchar(128) DEFAULT NULL, `Money` decimal(10,2) unsigned DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;插入一堆測試資料:
INSERT INTO `tbl_fin` VALUES (1, "2018-01-01", "小明", 100.00);INSERT INTO `tbl_fin` VALUES (2, "2018-01-01", "小明", 100.00);INSERT INTO `tbl_fin` VALUES (3, "2018-01-01", "小明", 100.00);INSERT INTO `tbl_fin` VALUES (4, "2018-01-02", "小明", 100.00);INSERT INTO `tbl_fin` VALUES (5, "2018-01-03", "小明", 100.00);INSERT INTO `tbl_fin` VALUES (6, "2018-01-05", "小紅", 100.00);INSERT INTO `tbl_fin` VALUES (7, "2018-01-06", "小紅", 100.00);INSERT INTO `tbl_fin` VALUES (8, "2018-01-08", "小紅", 100.00);INSERT INTO `tbl_fin` VALUES (9, "2018-02-08", "小紅", 100.00);INSERT INTO `tbl_fin` VALUES (10, "2018-03-08", "小紅", 100.00);INSERT INTO `tbl_fin` VALUES (11, "2018-04-08", "小紅", 100.00);INSERT INTO `tbl_fin` VALUES (12, "2018-05-08", "小紅", 100.00);INSERT INTO `tbl_fin` VALUES (13, "2018-06-08", "小紅", 100.00);INSERT INTO `tbl_fin` VALUES (14, "2018-06-08", "小明", 3.40);INSERT INTO `tbl_fin` VALUES (15, "2018-06-08", "小明", 398.49);INSERT INTO `tbl_fin` VALUES (16, "2018-06-08", "小明", 982.24);INSERT INTO `tbl_fin` VALUES (17, "2018-06-08", "小明", 715.72);INSERT INTO `tbl_fin` VALUES (18, "2018-05-08", "小明", 631.88);INSERT INTO `tbl_fin` VALUES (19, "2018-05-08", "小明", 12.25);INSERT INTO `tbl_fin` VALUES (20, "2018-05-08", "小明", 165.59);INSERT INTO `tbl_fin` VALUES (21, "2018-05-08", "小明", 791.23);INSERT INTO `tbl_fin` VALUES (22, "2018-04-08", "小明", 459.35);INSERT INTO `tbl_fin` VALUES (23, "2018-04-08", "小明", 923.06);INSERT INTO `tbl_fin` VALUES (24, "2018-04-08", "小明", 237.28);INSERT INTO `tbl_fin` VALUES (25, "2018-03-08", "小明", 417.18);INSERT INTO `tbl_fin` VALUES (26, "2018-03-08", "小明", 374.10);INSERT INTO `tbl_fin` VALUES (27, "2018-03-08", "小明", 618.93);INSERT INTO `tbl_fin` VALUES (28, "2018-03-08", "小明", 972.37);INSERT INTO `tbl_fin` VALUES (29, "2018-02-08", "小明", 5.04);INSERT INTO `tbl_fin` VALUES (30, "2018-02-08", "小明", 108.08);INSERT INTO `tbl_fin` VALUES (31, "2018-02-08", "小明", 525.31);INSERT INTO `tbl_fin` VALUES (32, "2018-01-08", "小明", 302.32);INSERT INTO `tbl_fin` VALUES (33, "2018-01-08", "小明", 935.65);INSERT INTO `tbl_fin` VALUES (34, "2018-01-08", "小明", 771.28);INSERT INTO `tbl_fin` VALUES (35, "2018-01-09", "小明", 49.48);INSERT INTO `tbl_fin` VALUES (36, "2018-01-09", "小紅", 933.54);INSERT INTO `tbl_fin` VALUES (37, "2018-01-09", "小紅", 519.26);INSERT INTO `tbl_fin` VALUES (38, "2018-01-09", "小紅", 795.69);INSERT INTO `tbl_fin` VALUES (39, "2018-01-09", "小紅", 420.67);然後可以進入主題了。
獲取明細賬:1、根據某使用者獲取某月流水賬內容:
假設某使用者等於“小明”,某月等於“2018-01”。
SELECT * FROM tbl_fin WHERE TrueName = "小明" AND DATE_FORMAT(fin_date,"%Y-%m") = "2018-01" ORDER BY fin_date ASC其中,
TrueName = "小明"表示篩選記錄中TrueName為小明的記錄
DATE_FORMAT(fin_date,"%Y-%m") 的意思是格式化fin_date的日期格式,輸出為YYYY-mm
ORDER BY fin_date ASC 意思是記錄根據fin_date從小到大排序。
獲取的結果就是小明再2018年01月的明細賬。
第二、就是月累計賬:假設需要獲取“小明”的每月賬單。
SELECT TrueName,SUM(Money) as Money,DATE_FORMAT(fin_date,"%Y-%m") as dateMonth FROM tbl_fin WHERE TrueName = "小明" GROUP BY CONCAT(TrueName,DATE_FORMAT(fin_date,"%Y-%m")) ORDER BY dateMonth ASCSUM(Money) as Money, 的意思是統計(加)Money的總金額,
DATE_FORMAT(fin_date,"%Y-%m") as dateMonth ,把日期格式化為年月的形式
GROUP BY CONCAT(TrueName,DATE_FORMAT(fin_date,"%Y-%m")) 這裡需要拆開來理解,
1、CONCAT(TrueName,DATE_FORMAT(fin_date,"%Y-%m")) 意思是把TrueName和年月日進行字符合並。
2、GROUP BY 意思是根據這個進行分組統計。
得出的結果就是小明每月累計的賬單情況:
上面只是一個初步的SQL教程,測試環境是MYSQL 5.5.47,SQL Server的寫法有所不同。
另外正常情況下,一般用於統計或篩選的,建議加上索引。特別是財務的統計計算。