 |
|
Code (PHP)
ตารางข้อมูล
CREATE TABLE `tb_request` (
`re_id` char(10) collate utf8_unicode_ci NOT NULL COMMENT 'รหัสการขอใช้งาน',
`re_date_go` date default NULL COMMENT 'วันเดินทางไป',
`re_time_go` time default NULL COMMENT 'เวลาเดินทางไป',
`re_date_back` date default NULL COMMENT 'วันเดินทางกลับ',
`re_time_back` time default NULL COMMENT 'เวลาเดินทางกลับ',
`zipcode_code` char(6) collate utf8_unicode_ci default NULL COMMENT 'จังหวัด',
`re_location` varchar(100) collate utf8_unicode_ci default NULL COMMENT 'สถานที่ไป',
`re_num` int(3) default NULL COMMENT 'จำนวนคน',
`car_id` char(10) collate utf8_unicode_ci default NULL COMMENT 'รหัสยานพาหนะ',
`per_id` char(10) collate utf8_unicode_ci NOT NULL COMMENT 'รหัสบุคลากร',
`re_driver` char(10) collate utf8_unicode_ci default NULL COMMENT 'พนักงานขับรถ',
`re_date` date NOT NULL COMMENT 'วันที่ขอใช้งาน',
`re_num_save` varchar(50) collate utf8_unicode_ci NOT NULL COMMENT 'เลขที่บันทึกข้อความ',
`re_status` enum('w','a','n','c') collate utf8_unicode_ci NOT NULL default 'w' COMMENT 'w = รอผลการตรวจสอบ a = ผ่านการอนุมัติ n = ไม่ผ่านการอนุมัติ c = ยกเลิก',
PRIMARY KEY (`re_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='ตารางขอใช้งาน';
--
-- dump ตาราง `tb_request`
--
INSERT INTO `tb_request` VALUES ('RE00000001', '2012-04-04', '00:10:00', '2012-04-04', '00:15:00', '730000', 'ช่อง 3', 50, 'C0001', 'PER0000001', '', '2012-04-30', '', 'w');
INSERT INTO `tb_request` VALUES ('RE00000002', '1469-05-05', '10:00:00', '1469-05-05', '09:25:00', '810000', 'ทะเล', 20, 'C0001', 'PER0000001', '', '2012-05-04', 'ก152', 'a');
INSERT INTO `tb_request` VALUES ('RE00000003', '2012-05-02', '00:05:00', '2012-05-02', '01:05:00', '810000', 'ทะเล', 60, 'C0001', 'PER0000005', '', '2012-05-04', '6456', 'w');
INSERT INTO `tb_request` VALUES ('RE00000004', '2012-05-10', '01:10:00', '2012-05-10', '01:30:00', '630000', 'ทะเล', 10, 'C0001', 'PER0000001', '', '2012-05-11', 'asd', 'a');
INSERT INTO `tb_request` VALUES ('RE00000005', '2012-05-12', '01:15:00', '2012-05-12', '01:20:00', '260000', 'ทะเล', 10, 'C0001', 'PER0000001', '', '2012-05-11', '545', 'a');
คำถามคือผมอยากจะได้รายงานตาม car_id ถ้าข้อมูลมี 10 แถวก็อยากให้แสดงทั้งหมด 10 แถว
จากคำสั่ง SQL นี้
Code (PHP)
select
`Year`,
sum(`Jan`) as `Jan`,
sum(`Feb`) as `Feb`,
sum(`Mar`) as `Mar`,
sum(`Apr`) as `Apr`,
sum(`May`) as `May`,
sum(`Jun`) as `Jun`,
sum(`Jul`) as `Jul`,
sum(`Aug`) as `Aug`,
sum(`Sep`) as `Sep`,
sum(`Oct`) as `Oct`,
sum(`Nov`) as `Nov`,
sum(`Dec`) as `Dec`,
sum(`Total`) as `Total`
from
(select year(`re_date`) as `Year`,
case month(`re_date`) when 1 then count(`re_date`) else null end as `Jan`,
case month(`re_date`) when 2 then count(`re_date`) else null end as `Feb`,
case month(`re_date`) when 3 then count(`re_date`) else null end as `Mar`,
case month(`re_date`) when 4 then count(`re_date`) else null end as `Apr`,
case month(`re_date`) when 5 then count(`re_date`) else null end as `May`,
case month(`re_date`) when 6 then count(`re_date`) else null end as `Jun`,
case month(`re_date`) when 7 then count(`re_date`) else null end as `Jul`,
case month(`re_date`) when 8 then count(`re_date`) else null end as `Aug`,
case month(`re_date`) when 9 then count(`re_date`) else null end as `Sep`,
case month(`re_date`) when 10 then count(`re_date`) else null end as `Oct`,
case month(`re_date`) when 11 then count(`re_date`) else null end as `Nov`,
case month(`re_date`) when 12 then count(`re_date`) else null end as `Dec`,
count(`re_date`) as Total
from tb_request group by year(`re_date`), month(`re_date`))
as temp group by Year
ขอขอบคุณมา ณ ที่นี้ด้วยครับ
Tag : PHP, MySQL
|
|
 |
 |
 |
 |
Date :
2012-05-11 10:51:35 |
By :
iHacking |
View :
1088 |
Reply :
1 |
|
 |
 |
 |
 |
|
|
|
 |