我无法让这个SQL工作。基本上我想UPDATE
列sort_order
并将其设置为parents_sort_order
,它由同一个表上的子查询选择。这是我的SELECT
查询,显示了我需要的数据....
SELECT p.id,p.sort_order,p.parent_id, (
SELECT `sort_order` AS parent_sort_order
FROM `product`
WHERE id = p.parent_id
)
FROM `product` p
WHERE p.dealer_id !=0
AND p.vendor_id >100
谢谢!
答案 0 :(得分:4)
加入您选择的版本:
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
更新选项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
更新选项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