MySQL5.1 行列转换。需要转换的列是时间

现在有表t_trm_status,统计当月每天的早中晚扫描次数。
1 如果当天存在记录,1使用V表示,0使用X表示
2 如果当天无记录,使用 - 表示
期望结果如下:
日期 2013-12 2013-12-01 2013-12-02 2013-12-03 2013-12-04 ....12-31

终端ID 在线/总次数 早 中 晚 早 中 晚 早 中 晚 早 中 晚

TRM01 10/93 V V X X X X X X X - - -

TRM02 10/93 V V X X X X X X X - - -

TRM03 10/93 V V X X X X X X X - - -

-- --------------------------------------------------------
-- 主机 :127.0.0.1
-- 服务器版本 :5.1.61-community - MySQL Community Server (GPL)
-- 服务器操作系统 :Win32
-- HeidiSQL 版本 :7.0.0.4278
-- 创建 :2014-01-24
-- --------------------------------------------------------

CREATE TABLE IF NOT EXISTS `t_trm_status` (
`fId` int(11) NOT NULL AUTO_INCREMENT,
`fDate` date DEFAULT NULL,
`fTrm_fid` int(11) NOT NULL,
`fMorning` tinyint(4) DEFAULT '0',
`fNoon` tinyint(4) DEFAULT '0',
`fAfternoon` tinyint(4) DEFAULT '0',
PRIMARY KEY (`fId`),
UNIQUE KEY `UK_date_tid` (`fDate`,`fTrm_fid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `t_trm_status` (`fId`, `fDate`, `fTrm_fid`, `fMorning`, `fNoon`, `fAfternoon`) VALUES

(10659, '2014-01-31', 14, 0, 0, 1),
(10652, '2014-01-24', 14, 0, 0, 1),

(10632, '2014-01-04', 14, 0, 0, 1),

(10629, '2014-01-01', 14, 0, 0, 1),

(10294, '2014-01-31', 13, 0, 0, 1),
(10284, '2014-01-21', 13, 0, 0, 1),

(10281, '2014-01-18', 13, 0, 0, 1),

(10280, '2014-01-17', 13, 0, 0, 1),
(10275, '2014-01-12', 13, 0, 0, 1),

(10270, '2014-01-07', 13, 0, 0, 1),

(10264, '2014-01-01', 13, 0, 0, 1),
最新回答
咑勾勾→シ

2020-11-23 11:58:30

先更改字段fMorning,fNoon,fAfternoon类型为字符型,然后update t_trm_status set fMorning='V' where fMorning='1'