相关疑难解决方法(0)

在mysql中嵌套插入以进行标记

我想用一个sql语句在博客帖子中添加一个标签.

说我的表看起来如下:

tags
+-------+-----------+
| tagid | tag       |
+-------+-----------+
|     1 | news      | 
|     2 | top-story | 
+-------+-----------+

tag2post
+----+--------+-------+
| id | postid | tagid |     
+----+--------+-------+
|  0 |    322 |     1 |
+----+--------+-------+
Run Code Online (Sandbox Code Playgroud)

我想解决的问题是插入一个新标签,检索它的id,然后在一个sql语句中将这个新id插入到关系表中.

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 …
Run Code Online (Sandbox Code Playgroud)

mysql tags tagging

3
推荐指数
1
解决办法
1324
查看次数

标签 统计

mysql ×1

tagging ×1

tags ×1