gpt4 book ai didi

mysql - 如何在 MySQL 5.x 中使用多个联接更新 SELECT

转载 作者:行者123 更新时间:2023-11-29 16:31:33 26 4
gpt4 key购买 nike

我需要更新包含分类的表,如果存在匹配的文章,则将文章 ID 添加到每个记录中。通过匹配ref_id、dial_ids来定义。

现在我不知何故陷入了 UPDATE SELECT 查询。

SQL Fiddle with Structure

结构表分类:

ID, article_id

结构表文章:

article_id, dial_id, ref_id

结构表classifieds_attr:

attr_group_id, cl_id, value

以下查询为我提供了匹配的classified_ids:

-- 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 查询以使用相应的article_ids 更新分类?

最佳答案

MySQL 没有用于 UPDATEFROM 子句。我不知道 classified 是如何连接到其他表的,因此请填写 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

关于mysql - 如何在 MySQL 5.x 中使用多个联接更新 SELECT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53834132/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com