gpt4 book ai didi

Mysql INSERT INTO 来自另一个具有条件和限制的表

转载 作者:行者123 更新时间:2023-11-29 04:26:32 25 4
gpt4 key购买 nike

我有两个具有一对多关系的表 table1(一个),table2(多个)。

table1 has (t1_id) as KEY
table2 has (t2_id) as KEY and t2_t1_id as as refference to table1

现在在 table1 中,我需要一个列 (rand_t2_id),它包含 table2 中的 id,我需要不在乎哪个

这是我试过的查询

INSERT INTO table1 (rand_t2_id)
SELECT t2_id
FROM table2
WHERE table1.t1_id = table2.t2_t1_id;

如果需要,还需要在某处建立限制。我只需要 table2 中的一个 id

这里运气不好,有人知道解决办法吗?

最佳答案

我觉得你可以试试

UPDATE table1 t1 INNER JOIN table2 t2
ON t1.t1_id = t2.t2_t1_id;
SET t1.rand_t2_id = t2.t2_id

我假设您的 table1 上已经有一个名为 rand_t2_id 的列。

关于Mysql INSERT INTO 来自另一个具有条件和限制的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10293631/

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