带有变量的MySQL Query每个连接只能运行一次

时间:2014-01-22 21:11:09

标签: mysql sql variables

我有一个查询,用于根据时间戳和前一行的时差来查找会话。问题是,查询仅适用于首次执行...

修改后的查询以显示问题:

mysql> SELECT channel, time, latitude, longitude, time_diff FROM ( SELECT time, (@prev_time - time) AS time_diff, IF( (@prev_time - time) > 300, @prev_time := 9999999999,@prev_time := time) AS prev_time, latitude, longitude, channel FROM (SELECT @prev_time := 0) AS vars JOIN waypoints WHERE waypoints.channel = 25 ORDER BY time DESC ) AS tmp  ORDER BY time DESC;

+---------+------------+-----------+-----------+-------------+
| channel | time       | latitude  | longitude | time_diff   |
+---------+------------+-----------+-----------+-------------+
|      25 | 1390335836 | 52.051292 |  8.550871 | -1390335836 |
|      25 | 1390335832 | 52.051296 |  8.550889 |           4 |
|      25 | 1390335575 | 52.051300 |  8.550860 |         257 |
|      25 | 1390335571 | 52.051300 |  8.550869 |           4 |
|      25 | 1390335521 | 52.051285 |  8.550858 |          50 |
|      25 | 1390335517 | 52.051277 |  8.550858 |           4 |
|      25 | 1390335081 | 52.051258 |  8.550860 |         436 |
|      25 | 1390334746 | 52.051258 |  8.550877 |  8609665253 |
|      25 | 1390334742 | 52.051224 |  8.550899 |  8609665257 |
+---------+------------+-----------+-----------+-------------+
9 rows in set (0.00 sec)

mysql> SELECT channel, time, latitude, longitude, time_diff FROM ( SELECT time, (@prev_time - time) AS time_diff, IF( (@prev_time - time) > 300, @prev_time := 9999999999,@prev_time := time) AS prev_time, latitude, longitude, channel FROM (SELECT @prev_time := 0) AS vars JOIN waypoints WHERE waypoints.channel = 25 ORDER BY time DESC ) AS tmp  ORDER BY time DESC;

+---------+------------+-----------+-----------+----------------------+
| channel | time       | latitude  | longitude | time_diff            |
+---------+------------+-----------+-----------+----------------------+
|      25 | 1390335836 | 52.051292 |  8.550871 | 18446744072319215780 |
|      25 | 1390335832 | 52.051296 |  8.550889 |           8609664167 |
|      25 | 1390335575 | 52.051300 |  8.550860 |           8609664424 |
|      25 | 1390335571 | 52.051300 |  8.550869 |           8609664428 |
|      25 | 1390335521 | 52.051285 |  8.550858 |           8609664478 |
|      25 | 1390335517 | 52.051277 |  8.550858 |           8609664482 |
|      25 | 1390335081 | 52.051258 |  8.550860 |           8609664918 |
|      25 | 1390334746 | 52.051258 |  8.550877 |           8609665253 |
|      25 | 1390334742 | 52.051224 |  8.550899 |           8609665257 |
+---------+------------+-----------+-----------+----------------------+
9 rows in set (0.00 sec)

(结合WHERE time_diff< 300我得到第一次执行6结果和以下执行0结果)

任何想法?

这里是原始查询;

SELECT 
    channel, 
    time, 
    latitude, 
    longitude 
FROM ( 
    SELECT 
        time, 
        (@prev_time - time) AS time_diff, 
        IF( (@prev_time - time) > 300, @prev_time := 9999999999,@prev_time := time) AS prev_time, 
        latitude, 
        longitude, 
        channel 
    FROM 
        waypoints, (SELECT @prev_time := 0) AS vars 
    WHERE waypoints.channel = 25    
    ORDER BY time DESC 
) AS tmp 

WHERE tmp.time_diff < 300 
ORDER BY time DESC 

这是相关表的转储:https://gist.github.com/denyskoch/8567707

1 个答案:

答案 0 :(得分:0)

感谢@Sebas的小提琴(对于mysql v5.1的不好,不会在数字错误上引发错误)我可以解决它:

你可以使用

SET sql_mode = 'NO_UNSIGNED_SUBTRACTION'; 

或更好的方式:

  • 制作 time字段已签名

问题与@prev_time - time有关,如下所示:UNSIGNED - UNSINGED,无符号划分的结果导致BIGINT溢出:)