在2个表中搜索唯一条目,然后更改数据

时间:2013-07-09 19:06:11

标签: php mysql

目标是搜索两个类似的表格以获取单行数据。

UPDATE
regusers_test AS rt
INNER JOIN
alt_toon_test AS att
ON
att.`toonname` = rt.`toonname`
SET
rt.`server` = CASE WHEN rt.`username`='$user' AND rt.`server`='$oserver' AND rt.`toonname`='$otoon' THEN '$server' ELSE rt.`server`
att.`server` = CASE WHEN att.`username`='$user' AND att.`server`='$oserver' AND att.`toonname`='$otoon' THEN '$server' ELSE att.`server`
WHERE
(
rt.`username`='$user' AND rt.`server`='$oserver' AND rt.`toonname`='$otoon'
)
OR
(
att.`username`='$user' AND att.`server`='$oserver' AND att.`toonname`='$otoon'
)

但我一直在接受:

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 'att.'server'=Case When att.'username'='$user'...

周末我做了很多谷歌搜索,但找不到解决方案。希望有人知道这是什么。

2 个答案:

答案 0 :(得分:0)

SET需要在作业之间。

编辑修正了您的查询:

UPDATE
regusers_test AS rt
INNER JOIN
alt_toon_test AS att
ON
att.`toonname` = rt.`toonname`
SET
rt.`server` = CASE WHEN rt.`username`='$user' AND rt.`server`='$oserver' AND rt.`toonname`='$otoon' THEN '$server' ELSE rt.`server`,
att.`server` = CASE WHEN att.`username`='$user' AND att.`server`='$oserver' AND att.`toonname`='$otoon' THEN '$server' ELSE att.`server`
WHERE
(
rt.`username`='$user' AND rt.`server`='$oserver' AND rt.`toonname`='$otoon'
)
OR
(
att.`username`='$user' AND att.`server`='$oserver' AND att.`toonname`='$otoon'
)

答案 1 :(得分:0)

您错过了

附近列之间的,
SET
rt.`server` = CASE WHEN rt.`username`='$user' AND rt.`server`='$oserver' AND rt.`toonname`='$otoon' THEN '$server' ELSE rt.`server` END ,
att.`server` = CASE WHEN att.`username`='$user' AND att.`server`='$oserver' AND att.`toonname`='$otoon' THEN '$server' ELSE att.`server` END