简体   繁体   中英

UPDATE query with left join not working

Hi I have tried different things to get it to work apparently I'm doing something wrong. Probably a small something.

So I'm trying to update a value from a left join tabel this is the select query witch works fine. But now I want to update the value.

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; 

This is the update query I'm trying to execute.

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; 

Can anybody help me out a bit here I don't understand what I'm doing wrong.

Well, it does not hurt to observe the syntax for update for a start:

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)); 

Looking at the query in more detail: there is no point in making the joins left joins, since the table you are updating is on the right hand side, so to update it, it should exist. So I would use inner joins instead of left. categories_description table does not seem to influence if content should be updated, so it should be removed from the SQL statement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM