基本上,我想在我正在建立的基本博客中包含“标签”或“标签”的概念。
“posts”表格如下所示:
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
中自动递增,而您技术知道在插入时插入post_id
的{{1}}。似乎不是最有效的解决方案。
有没有办法链接两个表......?
思想?
答案 0 :(得分:1)
从技术上讲,你不知道在插入时插入标签的post_id
当然,您可以使用LAST_INSERT_ID()
或PHP代码mysql_insert_id
。只要您对帖子进行INSERT
查询,就会知道生成的ID,并在插入标签/标签时使用它。这就是你链接表格的方式。