MySQL内部加入更新

时间:2013-07-26 20:13:11

标签: php sql database join mysqli

我正在尝试使用innjer连接执行关系更新,但似乎无法使语法正确。

行如下:

Catalogue.Category_ID
Catalogue.Visible

Categories.Category_ID
Categories.Category_Name

我正在尝试在类别ID号匹配时更新可见字段的值,并对应正确的名称。

UPDATE `Catalogue` 
SET `Visible` = '0' 
FROM `Catalogue` 
INNER JOIN `Categories` 
  ON Catalogue.Category_ID = Categories.Category_ID 
  AND Categories.Category_Name = 'Bases'

道歉,如果归结为一个愚蠢的语法错误,我对关系数据库不是最有经验的。

3 个答案:

答案 0 :(得分:17)

我相信这是正确的语法:

UPDATE `Catalogue` 
INNER JOIN `Categories` 
  ON Catalogue.Category_ID = Categories.Category_ID 
  AND Categories.Category_Name = 'Bases'
SET `Visible` = '0' 

答案 1 :(得分:3)

您使用TSQL语法,此处为MySQL

UPDATE `Catalogue` 
       INNER JOIN `Categories`
           ON Catalogue.Category_ID = Categories.Category_ID

SET `Visible` = '0' 
WHERE Categories.Category_Name = 'Bases'

如果您想缩短查询,请使用ALIAS

UPDATE Catalogue a 
       INNER JOIN Categories b
           ON a.Category_ID = b.Category_ID

SET    Visible = '0' 
WHERE  b.Category_Name = 'Bases'

答案 2 :(得分:1)

我相信你在寻找:

UPDATE 'Catalogue' SET 'Visible' = '0' FROM 'Catalogue' INNER JOIN 'Categories' ON Catalogue.Category_ID = Categories.Category_ID
WHERE Categories.Category_NAME = 'Bases'