看到我有一张桌子,它的字段,我在下面写道。查询后应该是一条记录。那是最短的一段时间。 dateend datestart的开始和结束。我有一张桌子:
广播 rows:id,datestart,dateend,namecategory,page 该表广播以下信息:
'1 ', '2012-01-09 13:00:00', '2012-01-09 15:00:00 ',' News', 'N1'
'2 ', '2012-01-09 16:00:00', '2012-01-09 17:00:00 ',' News', 'N1'
'3 ', '2012-01-09 11:00:00', '2012-01-09 12:00:00 ',' News', 'N2'
'4 ', '2012-01-09 16:00:00', '2012-01-09 18:00:00 ',' News', 'N2'
我可以撤销记录之间的时间最短的“页面”吗?
查询后应显示:
N1
通常可以实现这样的请求吗?虽然只是猜测这里没有min()不能做。
答案 0 :(得分:1)
SELECT
page
FROM
MyTable
ORDER BY
dateend - datestart
LIMIT 1
答案 1 :(得分:1)
SELECT
page
, MIN( (SELECT next.datestart - a.dateend
FROM TableX AS next
WHERE next.page = a.page
AND next.datestart >= a.dateend
AND next.id <> a.id
ORDER BY next.datestart ASC
LIMIT 1
)
) AS gap
FROM
TableX AS a
GROUP BY
page
ORDER BY
gap ASC
LIMIT 1
显示所有间隙,而不是计算最小间隙:
SELECT
page
, ( SELECT next.datestart - a.dateend
FROM TableX AS next
WHERE next.page = a.page
AND next.datestart >= a.dateend
AND next.id <> a.id
ORDER BY next.datestart ASC
LIMIT 1
) AS gapToNextBroadcast
FROM
TableX AS a
WHERE --- optional for
page = @pageToCheck --- one page only
答案 2 :(得分:0)
我认为只使用SQL可以做到这一点......或者它太复杂了。可能最好是在一些脚本中预先计算间隔并存储在缓存中或类似的东西中?