我有2张桌子。 table1
和table2
我希望插入值为table1
,然后将相同的值复制或插入table2
。
id
列为AUTO_INCREMENT
,但我不希望AUTO_INCREMENT
中有table2
。在插入id
之后,只需将table1
和其他列从table2
复制到table1
答案 0 :(得分:0)
如果您逐个记录插入值,则可以在单个事务中进行:
INSERTS INTO table1 (field1,field2...)
VALUES (value1,value2...);
SELECT @max_id:=max(id)
FROM table1;
INSERT INTO table2 (fields...)
VALUES (@max_id, values...);
COMMIT;
答案 1 :(得分:0)
$statement = $connection->prepare(
"INSERT INTO users (username, userid, password,type,created)
VALUES (:user_name_post, :shenase_post, :password_post, :userManger_post, NOW())
"
);
$statement->bindParam(":user_name_post",$userName);
$statement->bindParam(":shenase_post",$shenase);
$statement->bindParam(":password_post",$password);
$statement->bindParam(":userManger_post",$usermanager);
$stmt2 = $connection->prepare("SELECT @max_id:=max(id) FROM users");
$stmt2->execute();
$stm1=$connection->prepare("INSERT INTO historyuser (id,username, userid,type,data)
VALUES (@max_id,:user_name_post, :shenase_post,:userManger_post, NOW())");
$stm1->bindParam(":user_name_post",$userName);
$stm1->bindParam(":shenase_post",$shenase);
$stm1->bindParam(":userManger_post",$usermanager);