【发布时间】:2009-01-26 15:04:48
【问题描述】:
我想用一条 sql 语句为博文添加标签。
假设我的表格如下所示:
tags
+-------+-----------+
| tagid | tag |
+-------+-----------+
| 1 | news |
| 2 | top-story |
+-------+-----------+
tag2post
+----+--------+-------+
| id | postid | tagid |
+----+--------+-------+
| 0 | 322 | 1 |
+----+--------+-------+
我要解决的问题是插入一个新标签,检索它的id,然后将这个新的id插入到关系表中 > 在单个 sql 语句中。
INSERT INTO tag2post (postid, tagid)
VALUES
(
332, # the post
IF (
(SELECT tagid FROM tags WHERE tag = 'new_tag'),
(SELECT tagid FROM tags WHERE tag = 'new_tag'),
# here is where i'd like to insert
# the new_tag and return it's id
'i am lost here'
)
)
【问题讨论】: