开发者

loop not working in mysql Stored procedure

    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;
              开发者_如何学C      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;

If i am removing the statement

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;

then loop works fine and i am getting data for all the device but after adding that afore said statement i am getting data for only one device that means the loop is not continuing but i want to loop through all the device with the above statement. Please Help me ..Thanks in advance.


I have two notes:

...
INNER JOIN user_attributes ua ON u.id = ua.user_id AND ua.attribute_id = 1 AND u.id = user_id
...
  1. 'u.id = user_id' - if 'user_id' is a declared variable, it should be renamed because there is a field with the same name 'user_id'

  2. ua.attribute_id = 1 - shouldn't it be in a WHERE clause?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜