How to UPDATE and set values to a column in a sub-query on the same table
I'm having trouble getting this SQL to work. Basically I want to UPDATE
the column sort_order
and set it to the parents_sort_order
which is selected by a subquery on the same table. Here's my SELECT
query showing the data I need....
SELECT p.id,p.sort_order,p.parent_id, (
SELECT `sort_order` AS parent_sor开发者_如何学Pythont_order
FROM `product`
WHERE id = p.parent_id
)
FROM `product` p
WHERE p.dealer_id !=0
AND p.vendor_id >100
Thanks!
Join version of your select:
SELECT p.id,p.sort_order,p.parent_id, b.sort_order as parent_sort_order
FROM `product` p join `product` b
on p.id = b.parent_id
WHERE p.dealer_id !=0
AND p.vendor_id >100
Update Option 1:
update product a, product b set a.sort_order = b.parent_sort_order
where a.id = b.parent_id
and a.dealer_id !=0
and a.vendor_id > 100
Update Option 2:
update product a left join product b on a.id = b.parent_id
set a.sort_order = b.parent_sort_order
where a.dealer_id !=0
and a.vendor_id > 100
精彩评论