3

我正在尝试为网页创建一个简单的类似博客的新闻短片。为此,我有三个表:一个包含帖子,另一个包含类别,另一个将帖子与类别链接(称为“关系”)。

我已经能够管理大多数查询,但我仍然无法理解:类别 blogroll。主要问题是我想列出每个特定帖子中的所有关系,但是因为我正在按特定关系进行过滤,所以它当然只显示这个。

示例:表 1(POSTS):

POST_ID POST_TITLE POST_CONTENT
1       TEST       TEST
2       TES2       TEST2

表 2(类别):

CAT_ID CAT_NAME
1      NEWS
2      PRODUCTS

表 3(关系):

REL_POST REL_CAT
1        1
1        2
2        2

所以,帖子 1 属于“新闻”和“产品”,如果我要求一个“产品”类别的博客,这应该是输出:

POST_ID POST_TITLE POST_CONTENT POST_CATEGORIES
1       TEST       TEST         1:NEWS,2:PRODUCTS
2       TES2       TEST2        2:PRODUCTS

相反,POST_CATEGORIES 只列出 2:PRODUCTS,因为我过滤了 REL_CAT 的查询。

这是我设法制作的示例代码:

SELECT `post_id`, `post_title`, `post_content`, GROUP_CONCAT(CONCAT(rel_cat,':', cat_name)) AS post_categories
FROM `posts`
LEFT JOIN `relations` ON (`rel_post` = `post_id`)
LEFT JOIN `categories` ON (`cat_id` = `rel_cat`)
WHERE rel_cat = 2

有人可以指出我做错了什么吗?我曾尝试查阅手册并在网上搜索,但大多数结果将我指向有相反问题的人(他们想从连接表中过滤结果),并且 mysql 手册可以用混淆的 PERL 编写,因为我读过。

提前致谢。

4

3 回答 3

2

您需要在RELATION表上进行额外的连接,因为您要搜索post包含特定内容的内容category,并且要显示该内容的所有类别post

SELECT  a.post_id, 
        a.post_title, 
        a.post_content, 
        GROUP_CONCAT(CONCAT(b.rel_cat,':', c.cat_name)) AS post_categories
FROM    posts a
        INNER JOIN relations b
            ON b.rel_post = a.post_id
        INNER JOIN categories c
            ON c.cat_id = b.rel_cat
        INNER JOIN RELATIONS d  
            ON  a.POST_ID = d.REL_POST  AND 
                d.REL_CAT = 2               -- <<== SEARCH HERE
GROUP   BY a.post_id, a.post_title, a.post_content

输出

╔═════════╦════════════╦══════════════╦═══════════════════╗
║ POST_ID ║ POST_TITLE ║ POST_CONTENT ║  POST_CATEGORIES  ║
╠═════════╬════════════╬══════════════╬═══════════════════╣
║       1 ║ TEST       ║ TEST         ║ 1:NEWS,2:PRODUCTS ║
║       2 ║ TES2       ║ TEST2        ║ 2:PRODUCTS        ║
╚═════════╩════════════╩══════════════╩═══════════════════╝
于 2013-05-30T00:57:10.357 回答
0
SELECT `post_id`, `post_title`, `post_content`, GROUP_CONCAT(CONCAT(cat_id,':', cat_name)) AS post_categories
FROM `posts`
    JOIN `relations` r1 ON (r1.`rel_post` = `post_id`)
    JOIN `relations` r2 ON (r2.`rel_post` = `post_id`)
    JOIN `categories` ON (`cat_id` = r2.`rel_cat`)
WHERE r1.`rel_cat` = 2
GROUP BY `post_id`

SQLFIDDLE

于 2013-05-30T00:57:33.627 回答
0

我认为你正在寻找的是这样的:

SELECT `post_id`, `post_title`, `post_content`, GROUP_CONCAT(CONCAT(rel_cat,':', cat_name)) AS post_categories
FROM `posts`
    LEFT JOIN `relations` ON (`rel_post` = `post_id`)
    LEFT JOIN `categories` ON (`cat_id` = `rel_cat`)
GROUP BY `post_id`

您需要删除WHERE标准以获取两个/所有类别并添加GROUP BY.


如果您尝试仅过滤具有特定猫的那些帖子,但显示该帖子的所有猫,那么您需要再次加入表格:

SELECT post_id, post_title, post_content, GROUP_CONCAT(CONCAT(r.rel_cat,':', cat_name) ORDER BY r.rel_cat) AS post_categories
FROM posts
    LEFT JOIN relations r ON (r.rel_post = post_id)
    LEFT JOIN categories ON (cat_id = r.rel_cat)
    INNER JOIN relations r2 ON (r2.rel_post = post_id )
WHERE r2.rel_cat = 2
GROUP BY post_id
于 2013-05-30T00:42:53.627 回答