我的 mysql 查询有问题。
这是我的两张 table :
玩家位置:
ID | playerid | type | location
---|-----------------------
和用户:
ID | playername | [..]
----|--------------------
1 | example1 | ...
我想插入以下player_locations
:
ID | playerid | type | location
---|-----------------------
1 | 1 | 5 | DOWNTOWN
这就是我的查询:
INSERT INTO player_locations (id, type, location)
SELECT u1.ID as playerid,
d.type,
d2.location
FROM users u1
INNER JOIN users u2
ON 1 = 1
INNER JOIN (SELECT 5 as type
FROM DUAL) d
INNER JOIN (SELECT "DOWNTOWN" as location
FROM DUAL) d2
ON 1 = 1
WHERE u1.playername = "example1";
但是当我在 users
中有 6 行时,它会在 player_locations
中插入 6 行相同的行
最佳答案
为什么不直接写这个呢?
INSERT INTO player_locations(id, type, location)
SELECT u.ID as playerid, 5 as type, 'DOWNTOWN' as location
FROM users u
WHERE u.playername = 'example1';
自连接没有任何意义。您在查询中没有使用 u2
中的任何信息,因此它只是乘以行数。常量的额外连接是不必要的。
关于mysql - SQL 插入与连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27679783/