gpt4 book ai didi

php - 为博客构建 MySQL 数据库以包含 "labels"或 "tags"

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

基本上,我想在我正在构建的基本博客中包含“标签”或“标记”的概念。

“帖子”表如下所示:

table posts {
post_id (autoincrement, int, primary key)
post_text (text)
published (boolean)
timestamp (timestamp)
published_date (date)
user_id (varchar)
}

table labels {
post_id
label (varchar)
label_id (autoincrement, int, primary key)
}

这是最好的方法吗?

我在这种方法中看到的主要“问题”是在插入新帖子时,因为 post_id 在表 posts 中自动递增,而您却没有从技术上知道要在插入时插入到labels中的post_id。似乎不是最有效的解决方案。

有没有办法链接两个表...?

想法?

最佳答案

you don't technically know the post_id to insert into labels at the moment of insertion

当然可以,它位于 LAST_INSERT_ID() 中,或者来自您的 PHP 代码 mysql_insert_id 。一旦您对帖子进行 INSERT 查询,您就知道生成的 ID,并可以在插入标签/标签时使用它。这就是链接表格的方式。

关于php - 为博客构建 MySQL 数据库以包含 "labels"或 "tags",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7005800/

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