0

我试图将数据从一个表移动到另一个表,因为项目的需求已经改变。我认为 LOOP 可以工作,但我不断收到错误(它让我很恼火非描述性的 mysql 错误消息是如何的)。

这是我的代码:

SET @resid := 0;
SET @total := (SELECT COUNT(*) FROM reservations) + 1;
BEGIN
    label1: LOOP
    IF @resid < @total THEN
        SET @resid = @resid + 1
        INSERT INTO reservationbody(reservationid, time_in, time_out, ToLocation, FromLocation, startkm, endkm)
        VALUES(
            (SELECT
                reservationid,
                time_in,
                time_out,
                ToLocation,
                FromLocation,
                startkm,
                endkm
            FROM reservations WHERE reservationid = @resid)
        );
        INSERT INTO reservationbody(reservationid, time_in, time_out, ToLocation, FromLocation, startkm, endkm)
        VALUES(
            (SELECT
                reservationid,
                time_in,
                time_out,
                FromLocation,
                'Home',
                startkm,
                endkm
            FROM reservations WHERE reservationid = @resid)
        );
    END IF;
    LEAVE lable1;
    END LOOP
END;

这应该为reservations 表中的每一行在reservationbody 表中创建2 行。我认为问题出在循环上,但我已经根据http://dev.mysql.com/doc/refman/5.0/en/loop-statement.html上的 mysql 手动链接构建了这个查询

这是错误

0 ROW(s) affected

Execution TIME : 0.035 sec
Transfer TIME  : 0.002 sec
Total TIME     : 0.038 sec
---------------------------------------------------

0 ROW(s) affected

Execution TIME : 0.037 sec
Transfer TIME  : 0.001 sec
Total TIME     : 0.039 sec
---------------------------------------------------

QUERY:  BEGIN label1: LOOP IF @resid < @total THEN SET @resid = @resid + 1 INSERT INTO reservationbody(reservationid, time_in, time_out...

Error CODE: 1064
You have an error IN your SQL syntax; CHECK the manual that corresponds TO your MySQL SERVER VERSION FOR the RIGHT syntax TO USE near 'label1: Loop
        if @resid < @total then
            set @resid = @resid + 1
            INSERT INTO' AT line 2

Execution TIME : 0 sec
Transfer TIME  : 0 sec
Total TIME     : 0.039 sec
---------------------------------------------------

QUERY:  INSERT INTO reservationbody(reservationid, time_in, time_out, ToLocation, FromLocation, startkm, endkm) VALUES( (SELECT reserva...

Error CODE: 1136
COLUMN COUNT doesn't match value count at row 1

Execution Time : 0 sec
Transfer Time  : 0 sec
Total Time     : 0.040 sec
---------------------------------------------------

Query:  end if

Error Code: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'END IF' at line 1

Execution Time : 0 sec
Transfer Time  : 0 sec
Total Time     : 0.039 sec
---------------------------------------------------

Query:  leave lable1

Error Code: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LEAVE lable1' at line 1

Execution Time : 0 sec
Transfer Time  : 0 sec
Total Time     : 0.038 sec
---------------------------------------------------

Query:  end loop end

Error Code: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'END LOOP
    END' at line 1

Execution Time : 0 sec
Transfer Time  : 0 sec
Total Time     : 0.035 sec
---------------------------------------------------

谁能告诉我怎么了?

在此先感谢您的帮助!

4

1 回答 1

2

您可以简单地执行此操作且无需循环,只需执行两次 INSERT...SELECT 语句 -

INSERT INTO reservationbody(reservationid, time_in, time_out, ToLocation, FromLocation, startkm, endkm)
  SELECT reservationid, time_in, time_out, ToLocation, FromLocation, startkm, endkm FROM reservations;

INSERT INTO reservationbody(reservationid, time_in, time_out, ToLocation, FromLocation, startkm, endkm)
  SELECT reservationid, time_in, time_out, FromLocation, 'Home', startkm, endkm FROM reservations;
于 2012-05-14T11:00:50.327 回答