我必须生成2个报告:"按类别划分的费用"和"按子类别划分的费用"。由于我的银行结构如下:
CREATE TABLE `category` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`parent_id` int(11) DEFAULT NULL,
`desc_category` varchar(255) NOT NULL,
`user_id` int(11) NOT NULL DEFAULT '1',
`created` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `id` (`id`)
)
/----------------------------------------------------\
| id | parent_id | desc_category |user_id| created |
|----|-----------|-----------------|-------|---------|
| 1 | NULL | Residence | 9 | ... |
| 2 | 1 | Rent | 9 | ... |
| 3 | 1 | maintenance | 9 | ... |
| 4 | NULL | Person | 9 | ... |
| 5 | 4 | Alimentation | 9 | ... |
| 6 | 4 | school | 9 | ... |
\----------------------------------------------------/
报告"按子类别划分的费用"这样做了:
SELECT desc_category AS cat, value FROM cashbook
INNER JOIN category
ON cashbook.category_id = category.id_category
WHERE category.user_id = $user AND type_id = 2 AND MONTH(date) = $thismonth AND YEAR(date) = $thisyear
我如何按类别支出? (例如居住和人)
领取where parent_id is NULL
?