Сообщение #4 korshunov » 05.01.2021, 13:58
Примерно так (если я верно понял, ибо постановка вопроса точностью не отличается):
delete from ok_related_products where product_id in ( select DISTINCT id from ok_products where main_category_id=25);
INSERT INTO ok_related_products (product_id, related_id, position) select DISTINCT id, 111, 0 from ok_products where main_category_id=25;
INSERT INTO ok_related_products (product_id, related_id, position) select DISTINCT id, 222, 1 from ok_products where main_category_id=25;
INSERT INTO ok_related_products (product_id, related_id, position) select DISTINCT id, 333, 2 from ok_products where main_category_id=25;
INSERT INTO ok_related_products (product_id, related_id, position) select DISTINCT id, 444, 3 from ok_products where main_category_id=25;
INSERT INTO ok_related_products (product_id, related_id, position) select DISTINCT id, 555, 4 from ok_products where main_category_id=25;
Примерно так (если я верно понял, ибо постановка вопроса точностью не отличается):
delete from ok_related_products where product_id in ( select DISTINCT id from ok_products where main_category_id=25);
INSERT INTO ok_related_products (product_id, related_id, position) select DISTINCT id, 111, 0 from ok_products where main_category_id=25;
INSERT INTO ok_related_products (product_id, related_id, position) select DISTINCT id, 222, 1 from ok_products where main_category_id=25;
INSERT INTO ok_related_products (product_id, related_id, position) select DISTINCT id, 333, 2 from ok_products where main_category_id=25;
INSERT INTO ok_related_products (product_id, related_id, position) select DISTINCT id, 444, 3 from ok_products where main_category_id=25;
INSERT INTO ok_related_products (product_id, related_id, position) select DISTINCT id, 555, 4 from ok_products where main_category_id=25;