我有两个表用户和电子邮件:
USER
+--------+-------+--------+--------+
| id (PK)| email | email2 | email3 |
+--------+-------+--------+--------+
EMAIL
+---------+--------------+---------------+
| id (PK) | user_id (FK) | email_address |
+---------+--------------+---------------+
我需要从users表中选择电子邮件,然后将它们作为行插入到电子邮件表中。
答案 0 :(得分:1)
如果您的电子邮件表ID具有身份,我会像这样做
INSERT INTO email (userid,email_address)
SELECT id, email FROM user
UNION SELECT id, email2 FROM user
UNION SELECT id, email3 FROM user
答案 1 :(得分:0)
使用union来做到这一点。
insert into email values(
select user_id, email from user
union
select user_id, email1 from user
union
select user_id, email2 from user
union
select user_id, email2 from user
)