gpt4 book ai didi

MySQL如何链接表

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

我正在尝试使用 mysql 创建一个待办事项网络应用程序。有人可以解释一下如何选择特定于某个用户的待办事项吗?如何将 users 表与 todos 表链接起来?一般建议是首选,而不是实际代码。

最佳答案

它被称为外键关系。事情是这样的

users table
-----------
id (auto increment primary key)
name
address
...


todos table
-----------
id (auto increment primary key)
user_id (id from users table - this is the foreign key and relation to the user)
todo_text
...

如果您想稍后选择用户的待办事项,则可以使用联接:

select todos.*
from todos
inner join users on users.id = todos.user_id
where users.name = 'john'

关于MySQL如何链接表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22248099/

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