简体   繁体   English

在MySQL中复制n到m的关系

[英]Copy n to m relation in MySQL

Suppose I habe two tables recipient and list . 假设我有两个表recipientlist recipient has the columns id , email and customer . recipient有列idemailcustomer list has the columns id , name and customer . list包含列idnamecustomer

Both tables are connected via recipient_list with the columns recipient_id and list_id . 两个表都通过recipient_list与列recipient_idlist_id

In both recipient and list I have data for the customer "Demo customer" with appropriate entries in the join table. recipientlist我都有customer “演示客户”的数据,并在连接表中包含相应的条目。

What I'm trying to do now is to copy these entries including the relationship to another customer. 我试图现在要做的就是将这些条目复制包括关系到另一个客户。 I am aware I can insert data using a select using the following schema: 我知道我可以使用以下模式使用select插入数据:

INSERT INTO table_name(column_list)
SELECT 
   select_list 
FROM 
   another_table;

With this I can copy the entries of recipient and list . 有了这个,我可以复制recipientlist的条目。 However this does not help me with duplicating the nm relations. 然而,这并没有帮助我复制nm关系。 How can I achieve that? 我怎样才能做到这一点?

Edit: Here's a short example of what I like to achieve: 编辑:这是我想要实现的一个简短示例:

Before: 之前:

recipient
+----+------------------+---------------+
| id |      email       |   customer    |
+----+------------------+---------------+
|  1 | test@example.org | Demo Customer |
+----+------------------+---------------+
list
+----+-----------+---------------+
| id |   name    |   customer    |
+----+-----------+---------------+
|  1 | demo list | Demo Customer |
+----+-----------+---------------+
recipient_list
+--------------+---------+
| recipient_id | list_id |
+--------------+---------+
|            1 |       1 |
+--------------+---------+

After: 后:

recipient
+----+------------------+---------------+
| id |      email       |   customer    |
+----+------------------+---------------+
|  1 | test@example.org | Demo Customer |
|  2 | test@example.org | Real Customer |
+----+------------------+---------------+
list
+----+-----------+---------------+
| id |   name    |   customer    |
+----+-----------+---------------+
|  1 | demo list | Demo Customer |
|  2 | demo list | Real Customer |
+----+-----------+---------------+
recipient_list
+--------------+---------+
| recipient_id | list_id |
+--------------+---------+
|            1 |       1 |
|            2 |       2 |
+--------------+---------+

Please note that the primary keys aren't necessarily as nice as in the example. 请注意,主键不一定与示例中的一样好。

The query to insert would be 要插入的查询是

INSERT INTO table_name(column_list)
SELECT 
   select_list 
FROM 
   another_table
WHERE customer = customer_name;

The "another_table" should be the join of recipient and list tables over customer_name. “another_table”应该是customer_name上的收件人和列表的连接。 Taking join would eliminate the bad cases arising due to nm relationship. 加入将消除由于nm关系引起的不良情况。

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

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