Select 然后为每个结果插入
Select and then insert for each result
我有这个table:
Table name: `user_offer_rels`
+----+--------+--------+-------+
| ID | userID | permID | state |
+----+--------+--------+-------+
| 1 | 1 | 1 | 0 |
| 2 | 1 | 2 | 0 |
| 3 | 2 | 1 | 0 |
| 4 | 2 | 2 | 0 |
+----+--------+--------+-------+
我想为这个查询的每个结果插入一条新记录:
SELECT DISTINCT userID FROM `user_offer_rels`
The results:
+--------+
| userID |
+--------+
| 1 |
| 2 |
+--------+
因此,对于每个用户 ID,我都需要做这样的事情:
INSERT INTO `user_offer_rels` (userID, permID, state) VALUES (`the userID record from above`, 4, 0)
我的记录太多,无法一一插入。
编辑:
运行此 INSERT 后 user_offer_rels
table 的预期结果:
+----+--------+--------+-------+
| ID | userID | permID | state |
+----+--------+--------+-------+
| 1 | 1 | 1 | 0 |
| 2 | 1 | 2 | 0 |
| 3 | 2 | 1 | 0 |
| 4 | 2 | 2 | 0 |
| 5 | 1 | 4 | 0 |
| 6 | 2 | 4 | 0 |
+----+--------+--------+-------+
而不是 INSERT ... VALUES ...
使用 INSERT ... SELECT ...
INSERT INTO user_offer_rels (userID, permID, state)
SELECT DISTINCT userID, 4, 0
FROM user_offer_rels
参见demo。
您可以使用 select 作为插入语句
CREATE TABLE user_offer_rels
(`ID` int AUTO_INCREMENT PRIMARY KEY , `userID` int, `permID` int, `state` int)
;
INSERT INTO user_offer_rels
(`ID`, `userID`, `permID`, `state`)
VALUES
(1, 1, 1, 0),
(2, 1, 2, 0),
(3, 2, 1, 0),
(4, 2, 2, 0)
INSERT INTO user_offer_rels (`userID`, `permID`, `state`) SELECT DISTINCT userID,4, 0 FROM user_offer_rels
SELECT * FROM user_offer_rels
ID | userID | permID | state
-: | -----: | -----: | ----:
1 | 1 | 1 | 0
2 | 1 | 2 | 0
3 | 2 | 1 | 0
4 | 2 | 2 | 0
5 | 1 | 4 | 0
6 | 2 | 4 | 0
db<>fiddle here
我有这个table:
Table name: `user_offer_rels`
+----+--------+--------+-------+
| ID | userID | permID | state |
+----+--------+--------+-------+
| 1 | 1 | 1 | 0 |
| 2 | 1 | 2 | 0 |
| 3 | 2 | 1 | 0 |
| 4 | 2 | 2 | 0 |
+----+--------+--------+-------+
我想为这个查询的每个结果插入一条新记录:
SELECT DISTINCT userID FROM `user_offer_rels`
The results:
+--------+
| userID |
+--------+
| 1 |
| 2 |
+--------+
因此,对于每个用户 ID,我都需要做这样的事情:
INSERT INTO `user_offer_rels` (userID, permID, state) VALUES (`the userID record from above`, 4, 0)
我的记录太多,无法一一插入。
编辑:
运行此 INSERT 后 user_offer_rels
table 的预期结果:
+----+--------+--------+-------+
| ID | userID | permID | state |
+----+--------+--------+-------+
| 1 | 1 | 1 | 0 |
| 2 | 1 | 2 | 0 |
| 3 | 2 | 1 | 0 |
| 4 | 2 | 2 | 0 |
| 5 | 1 | 4 | 0 |
| 6 | 2 | 4 | 0 |
+----+--------+--------+-------+
而不是 INSERT ... VALUES ...
使用 INSERT ... SELECT ...
INSERT INTO user_offer_rels (userID, permID, state)
SELECT DISTINCT userID, 4, 0
FROM user_offer_rels
参见demo。
您可以使用 select 作为插入语句
CREATE TABLE user_offer_rels (`ID` int AUTO_INCREMENT PRIMARY KEY , `userID` int, `permID` int, `state` int) ; INSERT INTO user_offer_rels (`ID`, `userID`, `permID`, `state`) VALUES (1, 1, 1, 0), (2, 1, 2, 0), (3, 2, 1, 0), (4, 2, 2, 0)
INSERT INTO user_offer_rels (`userID`, `permID`, `state`) SELECT DISTINCT userID,4, 0 FROM user_offer_rels
SELECT * FROM user_offer_rels
ID | userID | permID | state -: | -----: | -----: | ----: 1 | 1 | 1 | 0 2 | 1 | 2 | 0 3 | 2 | 1 | 0 4 | 2 | 2 | 0 5 | 1 | 4 | 0 6 | 2 | 4 | 0
db<>fiddle here