0

我正在开发票务系统,我想为一组数据生成报告,如下所示:

  • 第一次票已分配给 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

我的查询可能不起作用。它返回一些不正确的空日期。我希望任何机构都会有所帮助。

4

1 回答 1

0

Using joins against subselects, with each one getting the relevant min or max:-

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')
于 2013-06-13T16:13:54.227 回答