read_loop:
LOOP
FETCH device_cur INTO device;
IF done1 THEN
LEAVE read_loop;
END IF;
set x = start;
repeat
SET tripcount = 0;
SET trip_previous = 0;
SELECT MAX(distinct(trip)) into tripcount from model_erv40 where date(log_time) = date(x) and device_id = device;
IF tripcount > 0 THEN
set y = 1;
repeat
SET cd = 0;
SELECT IFNULL(MAX(cumulative_distance), 0) into cd from model_erv40 where trip = y and date(log_time) = date(x) and device_id = device;
if cd > 0 then
if trip_previous = 0 then
set cdold = 0;
set cdcorrect = cd;
else
SELECT IFNULL(MAX(cumulative_distance), 0) into cdold from model_erv40 where trip = trip_previous and date(log_time) = date(x) and device_id = device;
set cdcorrect = cd - cdold;
end if;
SELECT users.id, users.verified INTO user_idv, verified FROM users INNER JOIN devices ON users.id = devices.user_id AND devices.id = device;
SELECT IFNULL(attribute_value, '') INTO group_code from user_attributes ua where ua.user_id = user_idv and ua.attribute_id = 1;
INSERT INTO trip_info(trip, user_id, device_id, IsVerified, groupcode, date, cumulative_distance) VALUES (y, user_idv, device, verified, IFNULL(group_code,''), x, ROUND(cdcorrect*0.0006214,2));
SET trip_previous = y;
end if;
set y = y + 1;
until y > tripcount
end repeat;
END IF;
set x = date_add(x, interval 1 day);
until x > enddate
end repeat;
END LOOP;
如果我删除声明
SELECT g.groupcode INTO group_code FROM users u INNER JOIN user_attributes ua ON u.id = ua.user_id AND ua.attribute_id = 1 AND u.id = user_id INNER JOIN groupcode_master g ON ua.attribute_value = g.groupcode;
然后循环工作正常,我正在获取所有设备的数据但是在添加上述声明之后我只获得一个设备的数据,这意味着循环没有继续,但我想循环通过所有设备与上述声明。请帮帮我..谢谢。
答案 0 :(得分:0)
我有两个笔记:
...
INNER JOIN user_attributes ua ON u.id = ua.user_id AND ua.attribute_id = 1 AND u.id = user_id
...
'u.id = user_id' - 如果'user_id'是声明的变量,则应重命名,因为有一个名称为'user_id'的字段
ua.attribute_id = 1 - 不应该在WHERE子句中吗?