1
+ create database if not exists ` moneytracker` ;
1
2
show databases;
2
3
use ` moneytracker` ;
3
4
show tables;
@@ -17,52 +18,25 @@ DESCRIBE `records`;
17
18
-- DROP TABLE `records`;
18
19
19
20
INSERT INTO ` records` (` user_id` , ` category` , ` date` , ` amount` , ` type` , ` detail` ) VALUES
20
- (1 , ' Food' , ' 2023-05-01' , 49 , ' Expense' , ' 早餐' ),
21
- (1 , ' Food' , ' 2023-05-01' , 90 , ' Expense' , ' 午餐' ),
22
- (1 , ' Clothing' , ' 2023-05-02' , 1906 , ' Expense' , ' GU' ),
23
- (1 , ' Food' , ' 2023-05-02' , 100 , ' Expense' , ' 晚餐' ),
24
- (1 , ' Housing' , ' 2023-05-03' , 6000 , ' Expense' , ' 房租' ),
25
- (1 , ' Housing' , ' 2023-05-03' , 520 , ' Expense' , ' 水電費' ),
26
- (1 , ' Transport' , ' 2023-05-04' , 100 , ' Expense' , ' 機車加油' ),
27
- (1 , ' Recreation' , ' 2023-05-06' , 400 , ' Expense' , ' 電影票' ),
28
- (1 , ' Healthcare' , ' 2023-05-07' , 180 , ' Expense' , ' 感冒' ),
29
- (1 , ' Others' , ' 2023-05-08' , 572 , ' Expense' , ' 生活用品' ),
30
- (1 , ' Part-Time' , ' 2023-05-10' , 500 , ' Income' , ' 代寫作業' ),
31
- (1 , ' Investment' , ' 2023-05-17' , 1482 , ' Income' , ' 股息' ),
32
- (1 , ' Healthcare' , ' 2023-05-07' , 680 , ' Expense' , ' 健身房月票' ),
33
- (1 , ' Others' , ' 2023-05-13' , 200 , ' Income' , ' 發票中獎' ),
34
- (1 , ' Food' , ' 2023-05-14' , 120 , ' Expense' , ' 晚餐' ),
35
- (1 , ' Clothing' , ' 2023-05-15' , 590 , ' Expense' , ' Uniqlo' ),
36
- (1 , ' Food' , ' 2023-05-15' , 180 , ' Expense' , ' 午餐' ),
37
- (1 , ' Transport' , ' 2023-05-12' , 290 , ' Expense' , ' 回彰化' ),
38
- (1 , ' Transport' , ' 2023-05-14' , 290 , ' Expense' , ' 回台南' ),
39
- (1 , ' Education' , ' 2023-05-18' , 390 , ' Expense' , ' 誠品買書' ),
40
- (1 , ' Recreation' , ' 2023-05-19' , 120 , ' Expense' , ' 遊樂園門票' ),
41
- (1 , ' Healthcare' , ' 2023-05-20' , 80 , ' Expense' , ' 購買藥品' ),
42
- (1 , ' Part-Time' , ' 2023-05-22' , 200 , ' Income' , ' 代寫作業' ),
43
- (1 , ' Investment' , ' 2023-05-23' , 5000 , ' Income' , ' 賣股票' ),
44
- (1 , ' Food' , ' 2023-05-26' , 100 , ' Expense' , ' 午餐' ),
45
- (1 , ' Clothing' , ' 2023-05-27' , 70 , ' Expense' , ' 購物' ),
46
- (1 , ' Recreation' , ' 2023-05-31' , 150 , ' Expense' , ' 休閒娛樂' ),
47
- (1 , ' Healthcare' , ' 2023-05-01' , 120 , ' Expense' , ' 感冒' ),
48
- (1 , ' Others' , ' 2023-05-02' , 70 , ' Expense' , ' 生活用品' ),
49
- (1 , ' Investment' , ' 2023-05-05' , 500 , ' Income' , ' 投資收益' ),
50
- (1 , ' Others' , ' 2023-05-07' , 500 , ' Expense' , ' 刮刮樂' ),
51
- (1 , ' Others' , ' 2023-05-07' , 700 , ' Income' , ' 刮刮樂' ),
52
- (1 , ' Food' , ' 2023-05-08' , 80 , ' Expense' , ' 早餐' ),
53
- (1 , ' Clothing' , ' 2023-05-09' , 70 , ' Expense' , ' 蝦皮' ),
54
- (1 , ' Transport' , ' 2023-05-11' , 100 , ' Expense' , ' 機車加油' ),
55
- (1 , ' Recreation' , ' 2023-05-13' , 150 , ' Expense' , ' 扭蛋' ),
56
- (1 , ' Others' , ' 2023-05-15' , 299 , ' Expense' , ' 生活用品' );
21
+ (1 , ' Food' , ' 2024-04-01' , 49 , ' Expense' , ' breakfast' ),
22
+ (1 , ' Food' , ' 2024-04-01' , 90 , ' Expense' , ' lunch' ),
23
+ (1 , ' Food' , ' 2024-04-02' , 100 , ' Expense' , ' dinner' ),
24
+ (1 , ' Housing' , ' 2024-04-03' , 6000 , ' Expense' , ' rent' ),
25
+ (1 , ' Investment' , ' 2024-04-23' , 5000 , ' Income' , ' sell stocks' ),
26
+ (1 , ' Food' , ' 2024-04-26' , 100 , ' Expense' , ' lunch' ),
27
+ (1 , ' Clothing' , ' 2024-04-27' , 70 , ' Expense' , ' Uniqlo' ),
28
+ (1 , ' Recreation' , ' 2024-04-18' , 300 , ' Expense' , ' watch movie' ),
29
+ (1 , ' Healthcare' , ' 2024-04-01' , 150 , ' Expense' , ' common cold' ),
30
+ (1 , ' Others' , ' 2024-04-02' , 70 , ' Expense' , ' daily supplies' ),
31
+ (1 , ' Transport' , ' 2024-04-11' , 100 , ' Expense' , ' refueling a scooter' ),
32
+ (1 , ' Others' , ' 2024-04-15' , 299 , ' Expense' , ' daily supplies' );
57
33
58
34
59
35
-- show all records
60
36
SELECT * FROM ` records` ;
61
- -- method 1, show Expense records of 1th user in May
62
- SELECT ` record_id` ,` category` ,` date` ,` amount` ,` type` ,` detail` FROM ` records` WHERE ` user_id` = 1 AND ` date` LIKE ' 2023-05%' AND ` type` = ' Expense' ;
63
- -- method 2, show Expense records of 1th user in May
64
- SELECT ` record_id` ,` category` ,` date` ,` amount` ,` type` ,` detail` FROM ` records` WHERE ` user_id` = 1 AND MONTH(` date` ) = 5 AND YEAR(` date` ) = 2023 AND ` type` = ' Expense' ;
65
- -- update record
66
- UPDATE ` records` SET ` category` = ' Food' , ` date` = ?, ` amount` = ?, ` type` = ' Expense' , ` detail` = ' 晚餐' WHERE ` user_id` = 1 AND ` record_id` = 1 ;
37
+ -- method 1, show Expense records of 1th user in April
38
+ SELECT ` record_id` ,` category` ,` date` ,` amount` ,` type` ,` detail` FROM ` records` WHERE ` user_id` = 1 AND ` date` LIKE ' 2024-04%' AND ` type` = ' Expense' ;
39
+ -- method 2, show Expense records of 1th user in April
40
+ SELECT ` record_id` ,` category` ,` date` ,` amount` ,` type` ,` detail` FROM ` records` WHERE ` user_id` = 1 AND MONTH(` date` ) = 4 AND YEAR(` date` ) = 2024 AND ` type` = ' Expense' ;
67
41
-- calculate the amount of specific user with specific type and data range
68
- SELECT SUM (` amount` ) FROM ` records` WHERE ` user_id` = 1 AND MONTH(` date` ) = 5 AND YEAR(` date` ) = 2023 AND ` type` = ' Expense' AND ` category` = ' Food' ;
42
+ SELECT SUM (` amount` ) FROM ` records` WHERE ` user_id` = 1 AND MONTH(` date` ) = 4 AND YEAR(` date` ) = 2024 AND ` type` = ' Expense' AND ` category` = ' Food' ;
0 commit comments