gpt4 book ai didi

MySQL 存储过程循环遍历 2 个表,选择值并存储在第 3 个表中

转载 作者:行者123 更新时间:2023-11-29 05:51:36 24 4
gpt4 key购买 nike

我有 3 个表 usersmemosmemos_read

用户:

+---+----------+
|ID | NAME |
+---+----------+
| 1 | Mike |
| 4 | Susan |
| 5 | Peter |
| 6 | Mary |
+---+----------+

备忘录:

+---+----------+
|ID | CONTENT |
+---+----------+
| 15| Hello |
| 16| World |
| 17| Foo |
| 23| Bar |
+---+----------+

我想创建一个带有嵌套循环的存储过程来为已阅读的备忘录创建一个“阅​​读 map ”,创建如下内容:

memos_read:

+---------+----------+
| MEMO_ID | USER_ID |
+---------+----------+
| 15 | 1 |
| 15 | 4 |
| 15 | 5 |
| 15 | 6 |
| 16 | 1 |
| 16 | 4 |
| 16 | 5 |
| 16 | 6 |
| 17 | 1 |
| 17 | 4 |
| 17 | 5 |
| 17 | 6 |
| 23 | 1 |
| 23 | 4 |
| 23 | 5 |
| 23 | 6 |
+---------+----------+

这可能吗?先谢谢你!

最佳答案

@尼克的answer应该为你工作;但是,如果您对创建新表不感兴趣,而是在查询中动态使用“读取 map ”;你可以使用 Derived Table .

一般查询结构如下:

SELECT
read_map.MEMO_ID,
read_map.USER_ID,
...
FROM
(
SELECT
m.ID AS MEMO_ID,
u.ID AS USER_ID
FROM users AS u
CROSS JOIN memos AS m
) AS read_map

... -- here you can join with other tables as required
WHERE ...

关于MySQL 存储过程循环遍历 2 个表,选择值并存储在第 3 个表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53441521/

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