gpt4 book ai didi

MYSQL从列中插入特定数据

转载 作者:行者123 更新时间:2023-11-29 21:31:10 25 4
gpt4 key购买 nike

我有一个名为 Pupils 的表和一个名为 Bus 的表。

巴士包含 ID、目的地(前往何处)和时间(何时出发)。

学生包含姓名、位置(居住地点)和 BusID(需要乘坐哪辆巴士)。

我想使用“巴士”表中的正确“ID”填充“巴士 ID”列(取决于学生选择的位置和时间)。

例如,学生住在特拉维夫,想要 10:00 乘车。

我需要在公交车表中使用相同的“ID”填充他的“BusID”列AND ROW(有很多学生),其中目的地等于特拉维夫,时间等于 10:00。

这有可能吗?我正在考虑使用插入/插入选择,但它并不真正符合我的需求。

最佳答案

正如我同时了解到的:学生们在其他地方(可能不在 T.A.)并且想要找到将他们带回 T.A. 的巴士。 10:00 点。

这可以通过以下方式实现

-- The id of the bus that travels at 10:00 to T.A.:
select id from bus where destination='Tel Aviv' and time='10:00:00';

-- Update the pupil's record with the bus-id:
update pupils set busid=... where location='Tel Aviv';

-- Together (and that's the answer):
update pupils set busid=(select id
from bus
where destination='Tel Aviv'
and time='10:00:00')
where location='Tel Aviv';

请注意,这假设第一个查询中只有一个结果行,即恰好有一个 10 点钟方向的巴士到 T.A。

关于MYSQL从列中插入特定数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35255416/

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