MySQL 8.0 या बाद के संस्करण के मामले में आप अगली क्वेरी का उपयोग कर सकते हैं:
INSERT IGNORE INTO products
SELECT
id_product,
143 as id_category,
(
SELECT MAX(position)
FROM products
WHERE id_category = 143
) +
(row_number() over (order by id_product)) as position
FROM products
WHERE id_category = 12;
SQLize.online से परिणाम :
+============+=============+==========+
| id_product | id_category | position |
+============+=============+==========+
| 190 | 12 | 10 |
+------------+-------------+----------+
| 191 | 12 | 11 |
+------------+-------------+----------+
| 230 | 12 | 12 |
+------------+-------------+----------+
| 15 | 143 | 12 |
+------------+-------------+----------+
| 150 | 143 | 50 |
+------------+-------------+----------+
| 190 | 143 | 51 |
+------------+-------------+----------+
| 191 | 143 | 52 |
+------------+-------------+----------+
| 230 | 143 | 53 |
+------------+-------------+----------+