左连接的UPDATE查询不起作用

时间:2015-10-05 13:56:59

标签: mysql

您好我尝试过不同的事情让它明显起作用我做错了什么。可能是一件小事。

所以我试图从左连接表中更新一个值,这是选择查询巫婆正常工作。但现在我想更新价值。

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; 

任何人都可以在这里帮助我,我不明白我做错了什么。

1 个答案:

答案 0 :(得分:0)

嗯,观察一个开始的更新语法没有坏处:

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语句中删除它。