简体   繁体   中英

MySQL - Insert into a table by querying product id from another table

I have two tables.

Here is the structure

CREATE TABLE IF NOT EXISTS `CATALOG_CATEGORY_PRODUCT` 
  ( 
     `CATEGORY_ID` INT(10) UNSIGNED NOT NULL DEFAULT '0' COMMENT 'Category ID', 
     `PRODUCT_ID`  INT(10) UNSIGNED NOT NULL DEFAULT '0' COMMENT 'Product ID', 
     `POSITION`    INT(11) NOT NULL DEFAULT '0' COMMENT 'Position', 
     PRIMARY KEY (`CATEGORY_ID`, `PRODUCT_ID`), 
     KEY `IDX_CATALOG_CATEGORY_PRODUCT_PRODUCT_ID` (`PRODUCT_ID`) 
  ) 
ENGINE=INNODB 
DEFAULT CHARSET=UTF8 
COMMENT='Catalog Product To Category Linkage Table'; 

CREATE TABLE IF NOT EXISTS `CATALOG_PRODUCT_ENTITY_TIER_PRICE` 
  ( 
     `VALUE_ID`          INT(11) NOT NULL AUTO_INCREMENT COMMENT 'Value ID', 
     `ENTITY_ID`         INT(10) UNSIGNED NOT NULL DEFAULT '0' COMMENT 
     'Entity ID', 
     `ALL_GROUPS`        SMALLINT(5) UNSIGNED NOT NULL DEFAULT '1' COMMENT 
     'Is Applicable To All Customer Groups', 
     `CUSTOMER_GROUP_ID` SMALLINT(5) UNSIGNED NOT NULL DEFAULT '0' COMMENT 
     'Customer Group ID', 
     `QTY`               DECIMAL(12, 4) NOT NULL DEFAULT '1.0000' COMMENT 'QTY', 
     `VALUE`             DECIMAL(12, 4) NOT NULL DEFAULT '0.0000' COMMENT 
     'Value', 
     `WEBSITE_ID`        SMALLINT(5) UNSIGNED NOT NULL COMMENT 'Website ID', 
     PRIMARY KEY (`VALUE_ID`) 
  ) 
ENGINE=INNODB 
DEFAULT CHARSET=UTF8 
COMMENT='Catalog Product Tier Price Attribute Backend Table'; 

So I have around 500 products.

I queried the product ids from the catalog_category_product where category_id = 37

This is the query.

SELECT PRODUCT_ID 
FROM   `CATALOG_CATEGORY_PRODUCT` 
WHERE  CATEGORY_ID = 37 

Using that result I would like to insert the price into tier price table.

INSERT INTO `CATALOG_PRODUCT_ENTITY_TIER_PRICE` 
            (`ENTITY_ID`, 
             `ALL_GROUPS`, 
             `CUSTOMER_GROUP_ID`, 
             `QTY`, 
             `VALUE`, 
             `WEBSITE_ID`) 
VALUES      (209, 
             1, 
             0, 
             60.0000, 
             10.0000, 
             0); 

Here catalog_product_entity_tier_price.entity_id = catalog_category_product.product_id

So 209 is the first result of catalog_category_product query. I would like to do the same thing for all results.

Can someone help me with mysql query to accomplish that?

Try INSERT INTO ... SELECT ... like this:

INSERT INTO `catalog_product_entity_tier_price` (`entity_id`, 
                                                 `all_groups`, 
                                                 `customer_group_id`,
                                                  `qty`, 
                                                  `value`, 
                                                  `website_id`)
SELECT product_id, 1, 0, 60.0000, 10.0000, 0
FROM `catalog_category_product`
WHERE category_id =37

You could do an INSERT INTO ... SELECT statement like this:

INSERT INTO `catalog_product_entity_tier_price` (`entity_id`, `all_groups`, `customer_group_id`, `qty`, `value`, `website_id`) 
SELECT product_id, 1, 0, 60.0000, 10.0000, 0 
  FROM `catalog_category_product`
  WHERE category_id =37

This would insert one row per result of your SELECT query. All other columns would be the same in all inserted rows.

If you want other values in the other columns, you would have to specify them someway.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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