Bạn có thể làm điều đó trong 3 phương pháp:
Trước & Đề xuất. SỬ DỤNG CHỌN TRONG GIÁ TRỊ INSERT:
INSERT INTO user (name)
VALUES ('John Smith');
INSERT INTO user_details (id, weight, height)
VALUES ((SELECT id FROM user WHERE name='John Smith'), 83, 185);
thứ hai. SỬ DỤNG LAST_INSERT_ID TRONG GIÁ TRỊ GIÁ TRỊ:
INSERT INTO a (id)
VALUES ('anything');
INSERT INTO user_details (id, weight, height)
VALUES (LAST_INSERT_ID(),83, 185);
Thứ ba. SỬ DỤNG PHP SCRIPT
<?php
// Connecting to database
$link = mysql_connect($wgScriptsDBServerIP, $wgScriptsDBServerUsername, $wgScriptsDBServerPassword, true);
if(!$link || [email protected]_SELECT_db($wgScriptsDBName, $link)) {
echo("Cant connect to server");
exit;
}
// Values to insert
$name = 'John Smith';
$weight = 83;
$height = 185;
// insertion to user table
$sql = "INSERT INTO user (name) VALUES ('$name')";
$result = mysql_query($sql,$conn);
// retrieve last id
$user_id = mysql_insert_id($conn);
mysql_free_result($result);
// insertion to user_details table
$sql = "INSERT INTO user_details (id, weight, height) VALUES ($user_id, $weight, $height)";
$result = mysql_query($sql,$conn);
mysql_free_result($result);
?>
thể bạn xin vui lòng gửi phần có liên quan của giản đồ? –