获取故障单mysql的最后进出时间

时间:2013-06-13 14:21:30

标签: mysql

我正在开发一个票务系统,我想为以下几组数据生成报告:

  • 第一张时间票已分配给IU池。
  • 上次故障单已分配给IU池。
  • 上次故障单已从IU池分配到任何其他池。
  • 修理时间(显示该游泳池最后一次停留的时间长度)
  • 票号。

这是我的表结构:

CREATE TABLE IF NOT EXISTS `aims_item_group_transfare` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `item_id` int(15) NOT NULL,
  `user_id` int(5) NOT NULL,
  `fromGroupId` int(3) NOT NULL,
  `toGroupId` int(3) NOT NULL,
  `transfareDate` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `status` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`),
  KEY `fromGroupId` (`fromGroupId`),
  KEY `toGroupId` (`toGroupId`),
  KEY `transfareDate` (`transfareDate`),
  KEY `item_id_ix` (`item_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1;

上表列出了不同池之间的转移时间。

这是我写的

SELECT FROM_UNIXTIME(MIN(TMIN.transfareDate)) AS 'First Time TO IU'
,FROM_UNIXTIME(MAX(TMAX.transfareDate)) AS 'Last Time TO IU', FROM_UNIXTIME(MAX(TMAXOut.transfareDate))'Last time from IU'
, A.item_id AS 'Ticket Number' 
,
CONCAT(
TIMESTAMPDIFF(day,FROM_UNIXTIME(MAX(TMAX.transfareDate)),FROM_UNIXTIME(MAX(TMAXOut.transfareDate))) , ' Days ',
MOD( TIMESTAMPDIFF(hour,FROM_UNIXTIME(MAX(TMAX.transfareDate)),FROM_UNIXTIME(MAX(TMAXOut.transfareDate))), 24), ' Hours ',
MOD( TIMESTAMPDIFF(minute,FROM_UNIXTIME(MAX(TMAX.transfareDate)),FROM_UNIXTIME(MAX(TMAXOut.transfareDate))), 60), ' Minutes ',
MOD( TIMESTAMPDIFF(minute,FROM_UNIXTIME(MAX(TMAX.transfareDate)),FROM_UNIXTIME(MAX(TMAXOut.transfareDate))), 60), ' Seconds'
) AS 'Time to repair'

FROM  aims_item_group_transfare T
LEFT JOIN  aims_items A ON T.item_id = A.item_id
LEFT JOIN  aims_item_group_transfare TMIN ON T.item_id = TMIN.item_id AND TMIN.toGroupId=20
LEFT JOIN  aims_item_group_transfare TMAX ON T.item_id = TMAX.item_id AND TMAX.toGroupId=20
LEFT JOIN  aims_item_group_transfare TMAXOut ON T.item_id = TMAXOut.item_id AND TMAXOut.fromGroupId=20
WHERE T.fromGroupId=20
AND T.transfareDate>=UNIX_TIMESTAMP('2013-06-05 00:00:00') AND T.transfareDate<=UNIX_TIMESTAMP('2013-06-12 23:59:59')
GROUP BY T.item_id

我的查询可能无效。它返回一些不正确的空日期。我希望任何人都能提供帮助。

1 个答案:

答案 0 :(得分:0)

对子选择使用连接,每个子选项获得相关的最小值或最大值: -

SELECT FROM_UNIXTIME(TMIN.MinTransfareDate) AS 'First Time TO IU',
FROM_UNIXTIME(TMAX.MaxTransfareDate) AS 'Last Time TO IU', 
FROM_UNIXTIME(TMAXOut.MaxFromTransfareDate)'Last time from IU',
A.item_id AS 'Ticket Number' ,
CONCAT(
    TIMESTAMPDIFF(DAY,FROM_UNIXTIME(TMAX.MaxTransfareDate),FROM_UNIXTIME(TMAXOut.MaxFromTransfareDate)) , ' Days ',
    MOD( TIMESTAMPDIFF(HOUR,FROM_UNIXTIME(TMAX.MaxTransfareDate),FROM_UNIXTIME(TMAXOut.MaxFromTransfareDate)), 24), ' Hours ',
    MOD( TIMESTAMPDIFF(MINUTE,FROM_UNIXTIME(TMAX.MaxTransfareDate),FROM_UNIXTIME(TMAXOut.MaxFromTransfareDate)), 60), ' Minutes ',
    MOD( TIMESTAMPDIFF(SECOND,FROM_UNIXTIME(TMAX.MaxTransfareDate),FROM_UNIXTIME(TMAXOut.MaxFromTransfareDate)), 60), ' Seconds'
) AS 'Time to repair'
FROM  aims_item_group_transfare T
LEFT JOIN  aims_items A ON T.item_id = A.item_id
LEFT JOIN (SELECT item_id, toGroupId, MIN(transfareDate) AS MinTransfareDate FROM aims_item_group_transfare GROUP BY item_id, toGroupId) TMIN ON T.item_id = TMIN.item_id AND TMIN.toGroupId = T.fromGroupId
LEFT JOIN (SELECT item_id, toGroupId, MAX(transfareDate) AS MaxTransfareDate FROM aims_item_group_transfare GROUP BY item_id, toGroupId) TMAX ON T.item_id = TMAX.item_id AND TMAX.toGroupId = T.fromGroupId
LEFT JOIN (SELECT item_id, fromGroupId, MAX(transfareDate) AS MaxFromTransfareDate FROM aims_item_group_transfare GROUP BY item_id, fromGroupId) TMAXOut ON T.item_id = TMAXOut.item_id AND TMAXOut.fromGroupId = T.fromGroupId
WHERE T.fromGroupId = 20
AND T.transfareDate>=UNIX_TIMESTAMP('2013-06-05 00:00:00') AND T.transfareDate<=UNIX_TIMESTAMP('2013-06-12 23:59:59')