我有一个看起来像这样的表: 用户(身份证,用户名,电子邮件,密码)
我想插入"关系"表: (id,userid1,userid2)
但这不起作用:
INSERT INTO relationship
WITH id1 AS (SELECT id FROM user WHERE USERNAME = "a"),
id2 AS (SELECT id FROM user WHERE USERNAME = "b")
SELECT id1.id, id2.id from id1, id2
我确定还有另一种方法可以做到这一点,但这似乎很干净,我无法弄清楚它为什么不起作用。我得到的错误是:
您的SQL语法有错误;查看与您的MySQL服务器版本对应的手册,以获得在' id1 AS
附近使用的正确语法答案 0 :(得分:0)
假设relationship.id是一个自动编号列,请尝试:
insert into relationship (userid1, userid2)
select id1, id2
from (
(select id as id1 from user where username='a') as a,
(select id as id2 from user where username='b') as b
);