[英]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. 所以我有大约500种产品。
I queried the product ids from the catalog_category_product
where category_id = 37
我从
catalog_category_product
查询了category_id = 37
的产品ID
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
这里
catalog_product_entity_tier_price.entity_id = catalog_category_product.product_id
So 209 is the first result of catalog_category_product
query. 所以209是
catalog_category_product
查询的第一个结果。 I would like to do the same thing for all results. 我想对所有结果做同样的事情。
Can someone help me with mysql query to accomplish that? 有人可以用mysql查询来帮我完成吗?
Try INSERT INTO ... SELECT ...
like this: 尝试
INSERT INTO ... SELECT ...
像这样:
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 ... SELECT
语句:
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. 这将为
SELECT
查询的每个结果插入一行。 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. 如果您想要其他列中的其他值,则必须以某种方式指定它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.