使用连接转换mysql select查询以使用连接更新查询

时间:2013-03-08 14:51:42

标签: mysql sql sql-update

我有一个像这样的选择查询:

SELECT n.nid, n.title, n2.nid, n2.title, n2.type
FROM node n
    JOIN og_ancestry o ON n.nid = o.group_nid
    JOIN node n2 ON n2.nid = o.nid 
    JOIN og_access_post a ON o.nid = a.nid
    JOIN content_type_group c ON n.vid = c.vid 
WHERE n.type = 'group'
    AND a.og_public = 1
    AND c.field_tyyppi_value != 'type1'

我无法弄清楚如何将其转换为更新查询。我试过这样:

UPDATE og_access_post a
    SET a.og_public = 0
FROM node n
    JOIN og_ancestry o ON n.nid = o.group_nid
    JOIN node n2 ON n2.nid = o.nid 
    JOIN og_access_post a ON o.nid = a.nid
    JOIN content_type_group c ON n.vid = c.vid 
WHERE n.type = 'group'
    AND a.og_public = 1
    AND c.field_tyyppi_value != 'type1'

但是我收到了一个错误:

  

您的SQL语法有错误;检查与您的MySQL服务器版本对应的手册,以便在'FROM n.nid,n.title,n2.nid

附近使用正确的语法

我找到了一些解决方案,但是我需要修改我的表和连接的顺序。选择查询中的逻辑是正确的,所以对我来说没有意义,为了做更新,我必须再次考虑查询的逻辑。

如何在不从根本上改变查询结构的情况下转换查询以进行更新?

4 个答案:

答案 0 :(得分:2)

UPDATE中的

MYSQL 加入不使用FROM子句,

update  og_access_post a 
        JOIN og_ancestry o ON o.nid = a.nid
        JOIN node n ON n.nid = o.group_nid
        JOIN node n2 ON n2.nid = o.nid
        JOIN content_type_group c ON n.vid = c.vid 
SET     a.og_public = 0 
WHERE   n.type = 'group' AND 
        a.og_public = 1 AND 
        c.field_tyyppi_value != 'type1'

答案 1 :(得分:0)

UPDATE og_access_post a
JOIN og_ancestry o 
    ON o.nid = a.nid
JOIN node n2 
    ON n2.nid = o.nid 
JOIN content_type_group c 
    ON n.vid = c.vid 
SET a.og_public = 0
WHERE n.type = 'group' 
    AND a.og_public = 1 
    AND c.field_tyyppi_value != 'type1'

答案 2 :(得分:0)

您需要执行类似

的操作
 update tableA set someColumn = 
        (SELECT someColumn 
            FROM tableA a
            where id=x
         )

答案 3 :(得分:0)

这样做的一种方法是:

UPDATE og_access_post a SET og_public = 0 
WHERE a.{PRIMARY_KEY} IN (SELECT a.{PRIMARY_KEY} FROM 
    node n JOIN og_ancestry o ON n.nid = o.group_nid
    JOIN node n2 ON n2.nid = o.nid 
    JOIN og_access_post a ON o.nid = a.nid
    JOIN content_type_group c ON n.vid = c.vid 
    WHERE n.type = 'group' AND 
    a.og_public = 1 AND c.field_tyyppi_value != 'type1')

我使用了。{PRIMARY_KEY},因为我无法判断你的主键是什么。