如何在MySQL5.x中使用多个连接更新select

zbdgwd5y  于 2021-06-15  发布在  Mysql
关注(0)|答案(1)|浏览(295)

我需要用分类广告更新一个表,如果存在匹配的文章,就向每个reacord添加文章id。通过匹配ref\u id、dial\u id定义。
现在我不知何故被updateselect查询困住了。
sql摆弄结构
结构表分类:

ID, article_id

结构表项目:

article_id, dial_id, ref_id

结构表分类属性:

attr_group_id, cl_id, value

以下查询提供了匹配的分类\u id:

-- get all classified ids where we have matching articles
SELECT
a.article_id,
ca_d.cl_id,
a.dial_id,
a.ref_id
FROM 
    `articles` a
-- get classified_ids where ref_id match the article
INNER JOIN classifieds_attr ca_r ON ca_r.attr_group_id = 8 AND ca_r.value = a.ref_id
-- filter down for matching dial
INNER JOIN classifieds_attr ca_d ON ca_r.cl_id = ca_d.cl_id AND ca_d.attr_group_id = 9 AND ca_d.value = a.dial_id

更改sql以更新具有匹配记录的分类表:

-- update classified table and insert article_id if matching article available
UPDATE c, a
SET 
    c.article_id = ca_d.cl_id,
    c.date_edited = ca.date_edited
FROM 
    classifieds c,
    articles a
-- get classified_ids where ref_id match the article
INNER JOIN classifieds_attr ca_r ON ca_r.attr_group_id = 8 AND ca_r.value = a.ref_id
-- filter down for matching dial
INNER JOIN classifieds_attr ca_d ON ca_r.cl_id = ca_d.cl_id AND ca_d.attr_group_id = 9 AND ca_d.value = a.dial_id

语法有点错误。如何修改sql查询来更新分类的toable和相应的article\u id?

drkbr07n

drkbr07n1#

mysql没有 FROM 条款 UPDATE . 我不知道怎么做 classified s连接到其他表,因此请填写 JOIN 条件。查询如下所示:

UPDATE articles a JOIN
       classifieds_attr ca_r
       ON ca_r.attr_group_id = 8 AND ca_r.value = a.ref_id JOIN
       classifieds_attr ca_d
       ON ca_r.cl_id = ca_d.cl_id AND ca_d.attr_group_id = 9 AND ca_d.value = a.dial_idclassifieds c JOIN
       classifieds c
       ON c.? = ?  -- whatever the right JOIN condition is
    SET c.article_id = ca_d.cl_id,
        c.date_edited = ca.date_edited

相关问题