[英]MySQL Inner Join two tables and insert row from one table to other on matching row field
So, I have to build a query to add a new row in customer_entity_varchar
for each entity_id
containing the data from the customer_address_entity_varchar
table. 所以,我必须建立一个查询中添加一个新行
customer_entity_varchar
每个entity_id
从包含数据customer_address_entity_varchar
表。
Table structure is as such: 表结构是这样的:
+----------+----------------+--------------+-----------+---------+
| value_id | entity_type_id | attribute_id | entity_id | value |
+----------+----------------+--------------+-----------+---------+
| 132 | 1 | 5 | 12 | John |
| 133 | 1 | 7 | 12 | Doe |
| 134 | 1 | 241 | 12 | 8675309 |
+----------+----------------+--------------+-----------+---------+
value_id
is autoincrement, generated by the table. value_id
是表自动生成的。
entity_type_id
will always be 1 (varchar). entity_type_id
将始终为1(varchar)。
attribute_id
should be set to 315. attribute_id
应该设置为315。
entity_id
will match between tables. entity_id
将在表之间匹配。
value of customer_address_entity_varchar
.value where customer_address_entity_varchar.attribute_id = 24
should be written in as a new row to customer_entity_varchar
. customer_address_entity_varchar
.value的值,其中customer_address_entity_varchar.attribute_id = 24
应该作为新行写入到customer_entity_varchar
。
Let me know if you need any more information. 让我知道您是否需要更多信息。
Here is a SQL fiddle with an attempt at an INSERT query solution: 这是尝试在INSERT查询解决方案上使用的SQL小提琴:
http://sqlfiddle.com/#!2/42ffdf/1 http://sqlfiddle.com/#!2/42ffdf/1
Having researched it further and tested it out, the answer I was looking for is: 在对其进行了进一步的研究和测试之后,我一直在寻找的答案是:
INSERT INTO customer_entity_varchar
(`entity_type_id`, `attribute_id`, `entity_id`, `value`)
SELECT 1 AS `entity_type_id`, 315 AS `attribute_id`, `entity_id`, `value`
FROM customer_address_entity_varchar WHERE `attribute_id` = 24
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.