简体   繁体   English

SQL 将同一表中的每个字段的新行插入表中

[英]SQL Insert into table new rows foreach field in same table

I have a database of categories, sub-categories and products.我有一个类别、子类别和产品的数据库。 Many sub-categories and therefore their products weren't adopted by the parent categories so I'm trying to use SQL to fix this but I'm coming across some issues.许多子类别,因此他们的产品没有被父类别采用,所以我试图使用 SQL 来解决这个问题,但我遇到了一些问题。

Said table has three columns;所述表具有三列; id_category, id_product, position (they are all ints) In this table every time a product is in a category, it's repeated for that id_product for each id_category of given categories. id_category、id_product、position(它们都是整数)在此表中,每次产品属于某个类别时,都会针对给定类别的每个 id_category 重复该 id_product。 Whether that be parent or sub-category.无论是父类别还是子类别。

As for an example, we can say the child category is 12 while the parent one is 143例如,我们可以说子类别是 12 而父类别是 143

So far what I have tried is到目前为止,我尝试过的是

SELECT id_product FROM category_products WHERE id_category = 12

This does give me the products I am interested to make new rows but I cannot manage to make the INSERT statement work.这确实给了我有兴趣制作新行的产品,但我无法使 INSERT 语句工作。

Secondly, position is also an issue as I need to select the last, highest number and for each field add a +1 to it, as it is the position of the product in the category.其次,position 也是一个问题,因为我需要 select 最后一个最高的数字,并为每个字段添加一个 +1,因为它是该类别中产品的 position。

What I am looking for is basically:我要找的基本上是:

  1. Take id_product where category = 12取 id_product where category = 12
  2. For each id_product taken make a row where category is equal to 143为每个 id_product 制作一行,其中 category 等于 143
  3. Take highest int in position where category = 143 and do +1 to it在 position 中取最高整数,其中类别 = 143 并对其执行 +1

Therefore we have something like this:因此我们有这样的事情:

+============+=============+==========+
| id_product | id_category | position |
+============+=============+==========+
| 190        | 12          | 10       |
+------------+-------------+----------+
| 191        | 12          | 11       |
+------------+-------------+----------+
| 230        | 12          | 12       |
+------------+-------------+----------+
| 15         | 143         | 12       |
+------------+-------------+----------+
| 150        | 143         | 50       |
+------------+-------------+----------+

AFTER THE SQL IT WOULD BE LIKE在 SQL 之后它会像

+============+=============+==========+
| 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       |
+------------+-------------+----------+

I tried several different syntaxes and everything but it's only returning errors to me all the time.我尝试了几种不同的语法和一切,但它一直只向我返回错误。 (This is done in PHPMyAdmin by the way). (顺便说一句,这是在 PHPMyAdmin 中完成的)。

In case MySQL 8.0 or later you can use next query:如果 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;

Result from SQLize.online : 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       |
+------------+-------------+----------+

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM