为什么我不能将多个语句放入单个事件中?

时间:2018-02-14 22:56:20

标签: mysql

我制作了一个需要调用8次的代码,每次使用不同的' x'值,范围从1到8.

UPDATE schedule
SET worker= (SELECT worker FROM (SELECT * FROM schedule) AS scheduleIn WHERE day = x + 1 AND scheduleIn.hour = schedule.hour)
WHERE day = x;

现在,就我看来,我可以用两种方法解决这个问题:

1)创建一个循环并使用每个X值调用代码,如下所示:(伪代码)

for(int x = 0; x < 8; x++)
{
UPDATE schedule
SET worker= (SELECT worker FROM (SELECT * FROM schedule) AS scheduleIn WHERE day = x + 1 AND scheduleIn.hour = schedule.hour)
WHERE day = x;
}

2)使用不同的值多次调用相同的代码。像这样:

UPDATE schedule
SET worker= (SELECT worker FROM (SELECT * FROM schedule) AS scheduleIn WHERE day = 1 + 1 AND scheduleIn.hour = schedule.hour)
WHERE day = 1;

UPDATE schedule
SET worker= (SELECT worker FROM (SELECT * FROM schedule) AS scheduleIn WHERE day = 2 + 1 AND scheduleIn.hour = schedule.hour)
WHERE day = 2;

UPDATE schedule
SET worker= (SELECT worker FROM (SELECT * FROM schedule) AS scheduleIn WHERE day = 3 + 1 AND scheduleIn.hour = schedule.hour)
WHERE day = 3;
...

你明白了。但两种选择都有其自身的问题。第一个问题是我仍然不知道如何在MySQL中制作类似循环的循环,尽管我从之前的帖子中获得了所有帮助。

第二个是:出于某种原因,似乎我不能在单个事件上放置多个语句。如果有人能告诉我自己做错了什么,我真的很感激。

Image1 Image2

0 个答案:

没有答案