我有两个表:projects和temp_projects(下面代码中的$ table)。我使用以下MySQL查询尝试使用项目中的数据更新temp_projects上的字段。
以下是查询:
$this->q("UPDATE $table, projects
CASE
WHEN $table.$number_field != projects.number
THEN set $table.$id_field = projects.id WHERE $table.old_proj_num = projects.number
WHEN $table.$number_field = projects.number
THEN SET $table.$id_field = projects.id WHERE $table.$number_field = projects.number
END");
我得到的错误是:
MySQL error 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 'CASE
WHEN temp_projects.number != projects.number
THEN set temp_proj' at line 2
When executing:
UPDATE temp_projects, projects
CASE
WHEN temp_projects.number != projects.number
THEN set temp_projects.project_id = projects.id WHERE temp_projects.old_proj_num = projects.number
WHEN temp_projects.number = projects.number
THEN SET temp_projects.project_id = projects.id WHERE temp_projects.number = projects.number
END
我遇到的问题是错误消息中的行"然后设置temp_proj'在第2行
之后应该读那么设置temp_projects.project_id = projects.id WHERE temp_projects.number = projects.number 。
我试图找出为什么在然后设置temp_proj
后切断查询的原因我不熟悉使用CASE命令,因此任何指针都会受到赞赏。
感谢。
答案 0 :(得分:0)
进行如下查询:
> UPDATE Students
SET NAME = CASE
> WHEN NAME = 'ABC' THEN 'MNO'
> WHEN NAME = 'DEF' THEN 'JKL'
> WHEN NAME = 'EFG' THEN 'GHI'
> ELSE NAME
> END WHERE LASTNAME IN ('ABC', 'DEF', 'EFG')
答案 1 :(得分:0)
喜欢这个吗?
$this->q("UPDATE $table, projects
set $table.$id_field =
CASE
WHEN $table.$number_field != projects.number
THEN projects.id WHERE $table.old_proj_num = projects.number
WHEN $table.$number_field = projects.number
THEN projects.id WHERE $table.$number_field = projects.number
END");
我的WHERE子句在两个WHEN语句中都不同,我需要根据$ table。$ number_field和projects.number是否相等来查找不同位置的数据。