gpt4 book ai didi

mysql - 左连接的更新查询不起作用

转载 作者:行者123 更新时间:2023-11-29 21:57:55 29 4
gpt4 key购买 nike

嗨,我尝试了不同的方法来让它工作,显然我做错了。可能是一件小事。

所以我试图从左连接表更新一个值,这是选择查询,效果很好。但现在我想更新该值。

SELECT 
categories.categories_id,
cd.categories_name as cat_name,
p2c.products_id as p2c_id,
p2c.categories_id ,
p.products_id as p_id,
pa.products_id as pa_id,
pa.c_content_id as pa_c_content_id,
pa.*,
c.group_id, c.content

FROM categories
left join (categories_description as cd) on(categories.categories_id = cd.categories_id)
left join (products_2_categories as p2c) on(categories.categories_id = p2c.categories_id)
left join (products as p) on (p2c.products_id = p.products_id)
left join (products_attributes as pa) on (p.products_id = pa.products_id and pa.attribute_items_id = 7)
left join (content as c) on(pa.c_content_id = c.group_id)

WHERE p2c.products_id IS NOT NULL AND pa.products_id IS NOT NULL AND (categories.categories_id = 42 OR categories.parent_id in(42)) ORDER BY cd.categories_name ASC , p2c.products_id ASC;

这是我尝试执行的更新查询。

UPDATE content 
FROM categories AS ca
LEFT JOIN(categories_description AS cd) ON(ca.categories_id = cd.categories_id)
LEFT JOIN(products_2_categories AS p2c) ON(ca.categories_id = p2c.categories_id)
LEFT JOIN(products AS p) ON(p2c.products_id = p.products_id)
LEFT JOIN(products_attributes AS pa) ON(p.products_id = pa.products_id AND pa.attribute_items_id = 7)
LEFT JOIN(content AS c) ON(pa.c_content_id = c.group_id)

SET content.content = ''

WHERE p2c.products_id IS NOT NULL
AND pa.products_id IS NOT NULL
AND (ca.categories_id = 42 OR ca.parent_id in(42))
ORDER BY cd.categories_name ASC , p2c.products_id ASC;

有人可以帮我一下吗,我不明白我做错了什么。

最佳答案

嗯,首先观察更新语法并没有什么坏处:

UPDATE categories AS ca
LEFT JOIN(categories_description AS cd) ON(ca.categories_id = cd.categories_id)
LEFT JOIN(products_2_categories AS p2c) ON(ca.categories_id = p2c.categories_id)
LEFT JOIN(products AS p) ON(p2c.products_id = p.products_id)
LEFT JOIN(products_attributes AS pa) ON(p.products_id = pa.products_id AND pa.attribute_items_id = 7)
LEFT JOIN(content AS c) ON(pa.c_content_id = c.group_id)

SET content.content = ''

WHERE p2c.products_id IS NOT NULL
AND pa.products_id IS NOT NULL
AND (ca.categories_id = 42 OR ca.parent_id in(42));

更详细地查看查询:进行左连接是没有意义的,因为您要更新的表位于右侧,因此要更新它,它应该存在。所以我会使用内连接而不是左连接。 categories_description表似乎不影响内容是否应该更新,因此应该从SQL语句中删除它。

关于mysql - 左连接的更新查询不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32950425/

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