早上好,我告诉他们我的情况: 我有两张桌子:
RATINGS:
GUIDE varchar(7)
USER varchar(20)
PRETEST decimal(4,3)
POSTEST decimal(4,3)
RATINGS_PERGUIDE:
USER varchar(20)
GUIDE1_PRETEST decimal(4,3)
GUIDE1_POSTEST decimal(4,3)
GUIDE2_PRETEST decimal(4,3)
GUIDE2_POSTEST decimal(4,3)3
.
.
.
GUIDE20_PRETEST decimal(4,3)
GUIDE20_POSTEST decimal(4,3)
表“RATINGS”有一个输入信息的表格,我需要将输入的信息移到“RATINGS_PERGUIDE”表中,我试试这个:
UPDATE RATINGS , RATINGS_PERGUIDE
IF RATINGS.USER = RATINGS_PERGUIDE.USER AND RATINGS.GUIDE = 'GUIDE 1'
THEN
SET RATINGS_PERGUIDE.GUIDE1_PRETEST = RATINGS.PRETEST
SET RATINGS_PERGUIDE.GUIDE1_POSTEST = RATINGS.POSTEST
ELSE IF RATINGS.USER = RATINGS_PERGUIDE.USER AND RATINGS.GUIDE = 'GUIDE 2'
THEN
SET RATINGS_PERGUIDE.GUIDE2_PRETEST = RATINGS.PRETEST;
SET RATINGS_PERGUIDE.GUIDE2_POSTEST = RATINGS.POSTEST;
.
.
.
END IF
但不起作用
即,根据“RATINGS”表中输入的“GUIDE”更新表“RATINGS_PERGUIDE”(24“GUIDES”不同)中的相应列
同样,如果可以在触发器中为插入表中的每个新记录完成“RATINGS”会更好
请帮助
答案 0 :(得分:1)
您正试图在update
中进行转移,这有点复杂。一种方法是简单地进行多次更新:
update RATINGS_PERGUIDE join
RATINGS
on RATINGS.USER = RATINGS_PERGUIDE.USER and
RATINGS.GUIDE = 'GUIDE 1'
SET RATINGS_PERGUIDE.GUIDE1_PRETEST = RATINGS.PRETEST,
RATINGS_PERGUIDE.GUIDE1_POSTEST = RATINGS.POSTEST;
修改并重复每个指南的更新。
如果您真的想在一个查询中执行此操作:
update RATINGS_PERGUIDE join
(select r.user,
max(case when r.GUIDE = 'GUIDE 1' then pretest end) as pretest_guide1,
max(case when r.GUIDE = 'GUIDE 1' then posttest end) as posttest_guide1,
max(case when r.GUIDE = 'GUIDE 2' then pretest end) as pretest_guide2,
max(case when r.GUIDE = 'GUIDE 2' then posttest end) as posttest_guide2,
. . .
from RATINGS r
group by r.user
) r
on r.USER = RATINGS_PERGUIDE.USER
SET RATINGS_PERGUIDE.GUIDE1_PRETEST = r.pretest_guide1,
RATINGS_PERGUIDE.GUIDE1_POSTEST = r.posttest_guide1,
RATINGS_PERGUIDE.GUIDE2_PRETEST = r.pretest_guide2,
RATINGS_PERGUIDE.GUIDE2_POSTEST = r.posttest_guide2,
. . .
答案 1 :(得分:0)
我通过简单地观察Phpmyadmin中“更新”的结构找到了解决方案
UPDATE RATINGS , RATINGS_PERGUIDE
SET RATINGS_PERGUIDE.GUIDE1_PRETEST = (CASE WHEN RATINGS.GUIDE = 'GUIDE 1' THEN RATINGS.PRETEST ELSE RATINGS_PERGUIDE.GUIDE1_PRETEST END),
RATINGS_PERGUIDE.GUIDE1_POSTEST = (CASE WHEN RATINGS.GUIDE = 'GUIDE 1' THEN RATINGS.PRETEST ELSE RATINGS_PERGUIDE.GUIDE1_POSTEST END),
RATINGS_PERGUIDE.GUIDE2_PRETEST = (CASE WHEN RATINGS.GUIDE = 'GUIDE 2' THEN RATINGS.PRETEST ELSE RATINGS_PERGUIDE.GUIDE2_PRETEST END),
RATINGS_PERGUIDE.GUIDE2_POSTEST = (CASE WHEN RATINGS.GUIDE = 'GUIDE 2' THEN RATINGS.PRETEST ELSE RATINGS_PERGUIDE.GUIDE2_POSTEST END),
. . . .
WHERE RATINGS.USER = RATINGS_PERGUIDE.USER
这对我来说,我把它放在存储过程中,然后用触发器调用
感谢All和Gordon Linoff