gpt4 book ai didi

php - 使用外键填充表

转载 作者:行者123 更新时间:2023-11-29 06:07:45 25 4
gpt4 key购买 nike

我有一个这样的架构

CREATE TABLE TrainManager(
train_name VARCHAR(5) REFERENCES Train(name),
station_id INT REFERENCES Station(station_id)
);

这两个引用表具有间接关系。

control (ctrl_id, train_name);

controlremote (ctrl_id, station_id);

如您所知,为了获取火车名称车站ID,我们需要进入其他两个表来比较ctrl_id除了比较 train_name 和 station_id 之外。

$query = "INSERT INTO `train` (train_name, station_id)
SELECT t.train_name, st.station_id
FROM train, station
WHERE t.train_name = ( SELECT c.train_name FROM control c
WHERE c.train_name = t.train_name)
AND
st.station_id = ( SELECT cr.station_id FROM controlremote cr
WHERE cr.station_id = st.station_id)
AND

但是我想不出一个合适的 SQL synatx 来比较 ctrl_id ...

最佳答案

INSERT INTO `train` (train_name, station_id)
SELECT control.train_name, controlremote.station_id
FROM control
LEFT JOIN controlremote ON control.ctrl_id = controlremote.ctrl_id

沿着这条路走下去。只需在 ctrl_id 上连接两个表即可。我希望我理解正确。

关于php - 使用外键填充表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10678844/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com