简体   繁体   English

MySQL:使用单个查询插入数据,但在非自动增量列上自动递增数字

[英]MySQL : Insert Data With Single Query, But Auto Increment Number On Non-Auto Increment Column

I have this SQL query : 我有这个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

it does multiple rows insertion to 'outbox' table. 它会在“发件箱”表中插入多行。 and I can easily get the ID number of first row by using this query : 我可以使用此查询轻松获取第一行的ID号:

SELECT LAST_INSERT_ID()

and let's say I have 532 as result from SELECT LAST_INSERT_ID() and I have 34 rows inserted. 让我说我有SELECT LAST_INSERT_ID()结果是532,我插入了34行。

how to use this 532 as initial number for other table named 'outbox_multipart' insertion and having it auto increment so the result will be like this : 如何使用这个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 .......                  |
|                                                                     |
+------+----------------+---------------------------------------------+ 

please note that outboxID column isn't auto increment column. 请注意,outboxID列不是自动增量列。 but it must have auto increment number from 532 + 34 rows = 566. 但它必须有532 + 34行= 566的自动增量编号。

Try with this 试试这个

ALTER TABLE outbox_multipart AUTO_INCREMENT = 532;

it will increments from the 532, 它将从532增加,

and remember that the "outboxID" should also be in auto increment initially 并记住“outboxID”最初也应该是自动增量

What about using a for loop combined with number of affected rows in mysql. 如何使用for循环结合mysql中受影响的行数。 If you are using PDO(which I reccommend) you can use this http://php.net/manual/en/pdostatement.rowcount.php 如果你正在使用PDO(我推荐)你可以使用这个http://php.net/manual/en/pdostatement.rowcount.php

So it would look something like this. 所以它看起来像这样。

<?php 

 $dbh = new pdo (your_database_connection_details);
 $stmt = dbh->prepare() //do your initial insert into the outbox table
 $stmt->bindParams() //bind your parameters
 $stmt->execute();

 $out_box_id =  dbh->lastInsertId();
 $row_count = dbh->rowCount();

for(x=0;x<$row_count;x++){   
  //insert $out_box_id into the outboxID column
  $stmt->execute();
  $out_box_id = $out_box_id + 1;
 }

So this should grab the last inserted id of your first insert and set the $out_box_id to it. 所以这应该抓住你第一次插入的最后插入的id并将$ out_box_id设置为它。 Then you can insert $out_box_id inside your for loop. 然后你可以在for循环中插入$ out_box_id。 The loop with run however many times the number of rows inserted was and then at the end of each loop it will increment the out_box_id by one. 运行的循环多次插入行数然后在每个循环结束时将out_box_id增加1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM