步骤1)在 users 表中找到player表中不存在的 id 。 步骤2)将缺少的 id s
插入到玩家表中所以我应该结合这两个查询。 (将SELECT查询的结果作为列表提供给INSERT INTO查询)
SELECT users.uid
FROM users
WHERE users.uid NOT IN (select uid from players);
INSERT INTO players (uid)
VALUES ()
最干净的方法是什么?
答案 0 :(得分:2)
将您的选择查询放在插入查询下方,然后移除values()
:
INSERT INTO players (uid)
SELECT users.uid
FROM users
WHERE users.uid NOT IN (select uid from players);
基于评论的编辑
一种简单的方法是使用INSERT IGNORE
- 这基本上会插入不在uid
表中的所有players
而忽略其余的:
INSERT IGNORE INTO players (uid)
SELECT users.uid
FROM users
答案 1 :(得分:0)
INSERT INTO players
SELECT users.uid
FROM users
WHERE users.uid NOT IN (select uid from players);
答案 2 :(得分:0)
试试这个:
INSERT INTO users (uid)
(SELECT users.uid
FROM users
LEFT JOIN players ON users.uid = players.uid)
WHERE players.uid IS null)