MySQL变量值在WHERE子句中未更改

时间:2019-05-30 03:04:59

标签: mysql variables intervals

我的变量@row_number的值在WHERE子句中未更改,但在SELECT子句中更新了

SET @row_number := -1;

SELECT (@row_number:=@row_number+1) as num,location_table.* FROM location_table
WHERE loc_userID = 'SP_EGMA001'
      AND (loc_date >= (STR_TO_DATE('05-27-2019 6:00:00', '%m-%d-%Y %H:%i:%s') + INTERVAL (30 * @row_number) minute))
ORDER BY loc_date asc

1 个答案:

答案 0 :(得分:0)

尝试此操作-在外部查询中访问@row_number别名num。任何语法错误的借口。

SET @row_number := -1;

select * from (
SELECT (@row_number:=@row_number+1) as num,location_table.* FROM location_table
WHERE loc_userID = 'SP_EGMA001' ) a
WHERE (loc_date >= (STR_TO_DATE('05-27-2019 6:00:00', '%m-%d-%Y %H:%i:%s') + INTERVAL (30 * num) minute))
ORDER BY loc_date asc