Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 使用SQL获取特定标记的所有帖子_Mysql_Sql_Codeigniter - Fatal编程技术网

Mysql 使用SQL获取特定标记的所有帖子

Mysql 使用SQL获取特定标记的所有帖子,mysql,sql,codeigniter,Mysql,Sql,Codeigniter,我有3个不同的表,我的博客工作 第一个表:博客帖子,它有一个ID、标题、内容、用户ID(创建帖子的人)、创建日期和slug 第二个表:blog_标签,它有一个ID、post_ID和tag_ID 第三张表:标签,它有一个ID和标签 我使用表3保存所有标记一次,这样就不会有重复的标记。然后我使用表2将标签连接到一个post(表1) 我遇到的问题是从一个特定标签获取所有帖子,同时返回所有其他标签 我的代码现在只返回我想在其中查找帖子的标记,但我仍然想写出其余的标记,只有显示的帖子必须包含特定的标记 我

我有3个不同的表,我的博客工作

第一个表:博客帖子,它有一个ID、标题、内容、用户ID(创建帖子的人)、创建日期和slug

第二个表:blog_标签,它有一个ID、post_ID和tag_ID

第三张表:标签,它有一个ID和标签

我使用表3保存所有标记一次,这样就不会有重复的标记。然后我使用表2将标签连接到一个post(表1)

我遇到的问题是从一个特定标签获取所有帖子,同时返回所有其他标签

我的代码现在只返回我想在其中查找帖子的标记,但我仍然想写出其余的标记,只有显示的帖子必须包含特定的标记

我通常对SQL很敏锐,但这次我的头脑完全静止了。。。请帮帮我:) 我正在使用PHP和CodeIgniter,如果这很重要的话

提前谢谢

迈克

编辑

我将结果打印为json,这给了我以下信息:

{
    "data": [
        {
            "id": "28",
            "title": "blabla",
            "content": "<p>hello<\/p>",
            "user_id": "1",
            "created_date": "2014-08-18 23:57:22",
            "slug": "blabla-2014-08-18-235722"
        },
        {
            "id": "34",
            "title": "test2",
            "content": "<p>test2<\/p>",
            "user_id": "1",
            "created_date": "2014-08-23 21:41:00",
            "slug": "test2-2014-08-23-214100"
        }
    ],
    "success": true
}
{
    "data": [
        {
            "id": "28",
            "title": "blabla",
            "content": "<p>hello<\/p>",
            "user_id": "1",
            "created_date": "2014-08-18 23:57:22",
            "slug": "blabla-2014-08-18-235722",
            "tags": [
                {
                    "id": 1
                    "tag": "test",
                },
                {
                    "id": 2
                    "tag": "test2",
                }
            ]
        },
        {
            "id": "34",
            "title": "test2",
            "content": "<p>test2<\/p>",
            "user_id": "1",
            "created_date": "2014-08-23 21:41:00",
            "slug": "test2-2014-08-23-214100"
            "tags": [
                {
                    "id": 3
                    "tag": "testa",
                },
                {
                    "id": 1
                    "tag": "test",
                }
            ]
        }
    ],
    "success": true
} 
我想得到的是:

{
    "data": [
        {
            "id": "28",
            "title": "blabla",
            "content": "<p>hello<\/p>",
            "user_id": "1",
            "created_date": "2014-08-18 23:57:22",
            "slug": "blabla-2014-08-18-235722"
        },
        {
            "id": "34",
            "title": "test2",
            "content": "<p>test2<\/p>",
            "user_id": "1",
            "created_date": "2014-08-23 21:41:00",
            "slug": "test2-2014-08-23-214100"
        }
    ],
    "success": true
}
{
    "data": [
        {
            "id": "28",
            "title": "blabla",
            "content": "<p>hello<\/p>",
            "user_id": "1",
            "created_date": "2014-08-18 23:57:22",
            "slug": "blabla-2014-08-18-235722",
            "tags": [
                {
                    "id": 1
                    "tag": "test",
                },
                {
                    "id": 2
                    "tag": "test2",
                }
            ]
        },
        {
            "id": "34",
            "title": "test2",
            "content": "<p>test2<\/p>",
            "user_id": "1",
            "created_date": "2014-08-23 21:41:00",
            "slug": "test2-2014-08-23-214100"
            "tags": [
                {
                    "id": 3
                    "tag": "testa",
                },
                {
                    "id": 1
                    "tag": "test",
                }
            ]
        }
    ],
    "success": true
} 
{
“数据”:[
{
“id”:“28”,
“标题”:“布拉布拉”,
“内容”:“你好”,
“用户id”:“1”,
“创建日期”:“2014-08-18 23:57:22”,
“slug”:“blabla-2014-08-18-235722”,
“标签”:[
{
“id”:1
“标签”:“测试”,
},
{
“id”:2
“标记”:“test2”,
}
]
},
{
“id”:“34”,
“标题”:“测试2”,
“内容”:“test2”,
“用户id”:“1”,
“创建日期”:“2014-08-23 21:41:00”,
“slug”:“test2-2014-08-23-214100”
“标签”:[
{
“id”:3
“tag”:“testa”,
},
{
“id”:1
“标签”:“测试”,
}
]
}
],
“成功”:真的吗
} 

我假设您很乐意向数据库发送两个请求

首先,获取给定标签的所有帖子:

SELECT * FROM blog_posts bp 
WHERE EXISTS (SELECT * FROM blog_tags bt INNER JOIN
               tags t ON t.id = bt.tag_id
              WHERE bp.id = bt.post_id
               AND t.tag = @SearchTag)
第二,我想,你想通过贴子将标签链接到你正在寻找的标签:

SELECT * FROM tags t
WHERE EXISTS ( -- Here we link two tags via blog_tags
               SELECT * FROM blog_tags bt1 INNER JOIN
               blog_tags bt2 ON bt1.post_id = bt2.post_id
                     AND bt1.tag_id != bt2.tag_id INNER JOIN
               tags t ON t.id = bt1.tag_id
               WHERE t.tag = @SearchTag
                  AND t.id = bt2.tag_id
)

我一气呵成,继续@Bulat的代码

SELECT *, 
GROUP_CONCAT(DISTINCT bt.tag_id) as tags_id, 
GROUP_CONCAT(DISTINCT t.tag) as tags
FROM blog_posts bp
INNER JOIN blog_tags bt
ON bt.post_id = bp.id
ON t.id = bt.tag_id
GROUP BY bt.post_id
ORDER BY bp.created_date DESC
然后,我将标记和标记id作为一个带有foreach循环的数组返回

$results = $this->db->query($sql)->result();

foreach($results as $result) {
  $result->tags_comma = $result->tags;
  strpos($result->tags, ',') ? $result->tags = explode(',', $result->tags) : $result->tags = array($result->tags);
  $result->tags_comma = str_replace(',', ', ', $result->tags_comma);
}

foreach($results as $result) {
  $result->tags_id_comma = $result->tags_id;
  strpos($result->tags_id, ',') ? $result->tags_id = explode(',', $result->tags_id) : $result->tags_id = array($result->tags_id);
  $result->tags_id_comma = str_replace(',', ', ', $result->tags_id_comma);
}

根据这篇文章,经过许多小时, 我找到了一种一次性完成的方法。到目前为止,它对我有效

请注意,我使用此表的名称: 通知作为博客帖子,标签作为博客标签,tasg作为标签

SELECT a.id, a.titulo, GROUP_CONCAT(tags.descripcion) as tags
FROM   noticias a
   INNER JOIN (SELECT   at.id_noticia
               FROM     tags_noticias at
                        INNER JOIN noticias a
                          ON a.id = at.id_noticia
                        INNER JOIN tags t
                          ON t.id = at.id_tag
               WHERE    t.descripcion IN ("tag1","tag2")
               GROUP BY at.id_noticia
               HAVING   Count(at.id_noticia) = 2) aa
     ON a.id = aa.id_noticia 
JOIN tags_noticias ON tags_noticias.id_noticia = a.id 
JOIN tags ON tags.id = tags_noticias.id_tag
GROUP BY tags_noticias.id_noticia

请编辑您的问题并显示(1)样本数据;(2) 预期结果;(3)您已经编写的代码。我在@Bulat的代码的帮助下更新了它。好的,没想到它需要两个请求,看看它,它看起来不错,但在检查答案是否正确之前,我会在实现时检查它是否有效。是否不可能按一个请求返回一个标记数组?是否希望每个blog_post记录都返回一个标记数组(例如逗号分隔的字符串)?是的,这会起作用,还有标记ID,然后我可以使用split使用它们。:)