sql >> Database >  >> RDS >> Mysql

SQL Voeg in tabel nieuwe rijen in voor elk veld in dezelfde tabel

In het geval dat MySQL 8.0 of hoger is, kunt u de volgende zoekopdracht gebruiken:

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;

Resultaat van 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       |
+------------+-------------+----------+



  1. PostgreSQL date() met tijdzone

  2. Installeer de SQL Server Agent-extensie op Azure Data Studio

  3. Hoe Atan2d() werkt in PostgreSQL

  4. Kan tabelnaam niet oplossen in de buurt van