在HAVING子句中计算

时间:2013-02-25 11:02:59

标签: mysql

我遇到以下SQL查询(MySQL)的问题:

SELECT CAST(LEFT(SUBSTR(STORE.VALUE, LOCATE('timestamp":', STORE.VALUE)+11),13) AS UNSIGNED) as starttime
FROM STORE
HAVING starttime <= MIN(starttime)+600000;

此查询返回1个值:1344543364317这是列starttime的最小值。

以下用于手动完成计算的查询返回更多值。

SELECT CAST(LEFT(SUBSTR(STORE.VALUE, LOCATE('timestamp":', STORE.VALUE)+11),13) AS UNSIGNED) as starttime
FROM STORE
HAVING starttime <= 1344543964317;

为什么此查询会返回更多值?

修改

有关数据的更多解释: 不幸的是,大多数数据都没用。基本上该表只包含两列:Key,Value。关键在这里并不重要。值包含以下内容:{"changeset":"Z:1>6b|5+6b$Welcome to Etherpad Lite!\n\nThis pad text is synchronized as you type, so that everyone viewing this page sees the same text. This allows you to collaborate seamlessly on documents!\n\nEtherpad Lite on Github: http://j.mp/ep-lite\n","meta":{"author":"","timestamp":1344543364317,"atext":{"text":"Welcome to Etherpad Lite!\n\nThis pad text is synchronized as you type, so that everyone viewing this page sees the same text. This allows you to collaborate seamlessly on documents!\n\nEtherpad Lite on Github: http://j.mp/ep-lite\n\n","attribs":"|6+6c"}}}

所以我基本上只是提取时间戳值并将其显示为名为starttime的虚拟列。 starttime列的前10个值是

1344543364317
1344543365939
1344543366439
1344543366454
1344543368794
1344553433259
1344553434054
1344553499779
1344553500871
1344553502104

2 个答案:

答案 0 :(得分:3)

您的值1344543964317大于4294967295,这是UNSIGNED整数的最大可能值。请改用BIGINT

答案 1 :(得分:0)

它不是很优雅,但我有一个解决方案。您似乎无法在等式的右侧使用startttime,因此您需要重做原始计算。

SELECT CAST(LEFT(SUBSTR(VALUE, LOCATE('timestamp":', VALUE)+11),13) AS UNSIGNED) AS starttime
FROM STORE
HAVING starttime <= 
    (SELECT MIN(CAST(LEFT(SUBSTR(STORE.VALUE, LOCATE('timestamp":', STORE.VALUE)+11),13) AS UNSIGNED)) FROM STORE) + 60000