所以我需要在兩個表中插入一個值,但我需要兩個表中的 id 相同,我試圖從第一個查詢中獲取重新調整的 ID 以將其傳遞給第二個查詢。這就是我到目前為止所擁有的。
INSERT
INTO
FantasyLeague.calendar (
round,
status,
race_initial,
race_name,
city,
country,
circuit_name,
start_date,
quali_deadline,
sprit_deadline,
race_deadline,
series_id)
VALUES (
new_round,
new_status,
new_race_initial,
new_race_name,
new_city,
new_country,
new_circuit_name,
new_start_date,
new_quali_deadline,
new_sprit_deadline,
new_race_deadline,
new_series_id)
RETURNING SET @id = race_id;
INSERT
INTO
FantasyLeague.races (race_id,
drivers_results,
series_id)
VALUES(
@id,
new_driver_results,
new_series_id
);
race_id 是 FantasyLeague.calendar 中的自動遞增 INT,但 FantasyLeague.races 中沒有。我嘗試使用這篇文章中的方法,但出現語法錯誤。
INSERT
INTO
FantasyLeague.calendar (
round,
status,
race_initial,
race_name,
city,
country,
circuit_name,
start_date,
quali_deadline,
sprit_deadline,
race_deadline,
series_id)
VALUES (
new_round,
new_status,
new_race_initial,
new_race_name,
new_city,
new_country,
new_circuit_name,
new_start_date,
new_quali_deadline,
new_sprit_deadline,
new_race_deadline,
new_series_id)
RETURNING race_id) )
INSERT
INTO
FantasyLeague.races (race_id,
drivers_results,
series_id)
SELECT
race_id,
new_driver_results,
new_series_id;
提前致謝。
uj5u.com熱心網友回復:
從未使用過 Maria,但我相信您會采用 MySQL 方法:
INSERT INTO FantasyLeague.races (
race_id,
drivers_results,
series_id)
VALUES(
LAST_INSERT_ID(),
new_driver_results,
new_series_id
);
LAST_INSERT_ID()回傳之前插入的 ID FantasyLeague.calendar,從而連接關系
您還可以將 ID 存盤在您使用的變數中(可能多次)..
INSERT INTO calendar ...;
SET @lii = LAST_INSERT_ID();
INSERT INTO FantasyLeague.races (
race_id,
drivers_results,
series_id
)
VALUES(
@lii,
new_driver_results,
new_series_id
);
INSERT INTO FantasyLeague.other(
race_id,
blah
)
VALUES(
@lii,
blah
);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/430043.html
上一篇:將一行從對偶拆分為幾個不同的列
