[英]Copy n to m relation in MySQL
假設我有兩個表recipient
和list
。 recipient
有列id
, email
和customer
。 list
包含列id
, name
和customer
。
兩個表都通過recipient_list
與列recipient_id
和list_id
。
在recipient
和list
我都有customer
“演示客戶”的數據,並在連接表中包含相應的條目。
我試圖現在要做的就是將這些條目復制包括關系到另一個客戶。 我知道我可以使用以下模式使用select插入數據:
INSERT INTO table_name(column_list)
SELECT
select_list
FROM
another_table;
有了這個,我可以復制recipient
和list
的條目。 然而,這並沒有幫助我復制nm關系。 我怎樣才能做到這一點?
編輯:這是我想要實現的一個簡短示例:
之前:
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 |
+--------------+---------+
后:
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 |
+--------------+---------+
請注意,主鍵不一定與示例中的一樣好。
要插入的查詢是
INSERT INTO table_name(column_list)
SELECT
select_list
FROM
another_table
WHERE customer = customer_name;
“another_table”應該是customer_name上的收件人和列表的連接。 加入將消除由於nm關系引起的不良情況。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.