我有这个 SQL 查询:
INSERT INTO db1.outbox (DestinationNumber, TextDecoded)
SELECT User.CellPhone, '$SMSMessage' as TextDecoded
FROM db2.User
WHERE User.PurchaseDate BETWEEN 2012-01-01 AND 2012-01-31
它会将多行插入到“发件箱”表中。我可以使用此查询轻松获取第一行的 ID 号:
SELECT LAST_INSERT_ID()
假设 SELECT LAST_INSERT_ID()
的结果为 532,并且插入了 34 行。
如何使用此 532 作为其他名为“outbox_multipart”插入的表的初始编号并使其自动递增,以便结果如下:
+------+----------------+----------+----------------------------------+
| ID | phonenumber | outboxID | message |
+------+----------------+---------------------------------------------+
| ...... |
| 1025 | 555-123456 | 532 | part 2 : hello there! |
| 1026 | 555-999999 | 533 | part 2 : hello there! |
| 1027 | 555-888888 | 534 | part 2 : hello there! |
| |
| ...... 34 rows inserted here ....... |
| |
+------+----------------+---------------------------------------------+
请注意,outboxID 列不是自动递增列。但它必须具有自动递增编号,从 532 + 34 行 = 566。
最佳答案
试试这个
ALTER TABLE outbox_multipart AUTO_INCREMENT = 532;
它将从 532 开始递增,
并记住“outboxID”最初也应该自动递增
关于php - MySQL : Insert Data With Single Query, 但非自动递增列上的数字自动递增,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12613999/