使用多个列嵌套选择MySQL

使用多个列嵌套选择MySQL,mysql,subquery,Mysql,Subquery,我目前正在尝试检索最新的帖子,以及每个帖子的相关帖子x号。我手头有以下问题: SELECT id, title, content (SELECT GROUP_CONCAT(title) FROM posts -- Select title of related posts WHERE id <> p.id AND id IN ( SELECT p_id FROM tagsmap

我目前正在尝试检索最新的帖子,以及每个帖子的相关帖子x号。我手头有以下问题:

SELECT id, title, content
       (SELECT GROUP_CONCAT(title) FROM posts        -- Select title of related posts 
        WHERE id <> p.id AND id IN (                 
           SELECT p_id FROM tagsmap                  -- Select reletad post ids from tagsmap
           WHERE t_id IN (
              SELECT t_id FROM tagsmap               -- Select the tags of the current post 
              WHERE p_id = p.id) 
           ) ORDER BY id DESC LIMIT 0, 3) as related 
FROM posts as p ORDER BY id DESC LIMIT 5
最后,这是我使用的查询。我删除了Where子句,因为它是不必要的,并且使用了LEFT JOIN而不是JOIN,因为否则它将忽略没有标记的帖子。最后,将DISTINCT添加到group_concat,因为它连接了重复的行。例如,如果一篇文章有多个公共标记与一篇相关的文章,那么它将导致重复的连接

上面的查询非常有效。谢谢大家。

像这样吗

SELECT id, title, content
       (SELECT GROUP_CONCAT(concat(cast(id as varchar(10)), ':', title)) FROM posts        -- Select title of related posts 
        WHERE id <> p.id AND id IN (                 
           SELECT p_id FROM tagsmap                  -- Select reletad post ids from tagsmap
           WHERE t_id IN (
              SELECT t_id FROM tagsmap               -- Select the tags of the current post 
              WHERE post_id = p.id) 
           ) ORDER BY id DESC LIMIT 0, 3) as related 
FROM posts as p ORDER BY id DESC LIMIT 5
像这样

SELECT id, title, content
       (SELECT GROUP_CONCAT(concat(cast(id as varchar(10)), ':', title)) FROM posts        -- Select title of related posts 
        WHERE id <> p.id AND id IN (                 
           SELECT p_id FROM tagsmap                  -- Select reletad post ids from tagsmap
           WHERE t_id IN (
              SELECT t_id FROM tagsmap               -- Select the tags of the current post 
              WHERE post_id = p.id) 
           ) ORDER BY id DESC LIMIT 0, 3) as related 
FROM posts as p ORDER BY id DESC LIMIT 5

好的-这将起作用,并且它还有一个额外的优点,即消除了子查询,当您获得大量记录时,子查询会使您的速度减慢:

SELECT p1.id, p1.title, p1.content,
    group_concat( p2.id) as 'P IDs',
    group_concat( p2.title) as 'P titles'
FROM posts as p1
JOIN tagsmap as tm1 on tm1.p_id = p1.id
JOIN tagsmap as tm2 on tm2.t_id = tm1.t_id and tm1.p_id <> tm2.p_id
JOIN posts as p2 on p2.id = tm2.p_id
WHERE p2.id <> p1.id
GROUP BY p1.id
ORDER BY p1.id desc limit 5;
我们在这里做的是从第一个版本的帖子中选择您想要的内容,通过post.id将它们连接到tagsmap,通过tag id将它们自连接到tagsmap以获得所有相关的标签,然后再连接回另一个帖子p2以获得这些相关标签指向的帖子


使用GROUP BY放弃所有加入的DUP,您就在那里了。

好的-这会起作用,并且它还有一个额外的优点,即消除了子查询,当您获得大量记录时,子查询会减慢您的速度:

SELECT p1.id, p1.title, p1.content,
    group_concat( p2.id) as 'P IDs',
    group_concat( p2.title) as 'P titles'
FROM posts as p1
JOIN tagsmap as tm1 on tm1.p_id = p1.id
JOIN tagsmap as tm2 on tm2.t_id = tm1.t_id and tm1.p_id <> tm2.p_id
JOIN posts as p2 on p2.id = tm2.p_id
WHERE p2.id <> p1.id
GROUP BY p1.id
ORDER BY p1.id desc limit 5;
我们在这里做的是从第一个版本的帖子中选择您想要的内容,通过post.id将它们连接到tagsmap,通过tag id将它们自连接到tagsmap以获得所有相关的标签,然后再连接回另一个帖子p2以获得这些相关标签指向的帖子


使用GROUP BY放弃所有加入的DUP,您就在那里。

您的post_id=p.id在哪里,是指p_id=p.id在哪里?我必须推断出你们桌子的结构,所以我的猜测可能是错误的。另外,您没有使用表标记-对吗?您有Where post_id=p.id的地方,您是指Where p_id=p.id吗?我必须推断出你们桌子的结构,所以我的猜测可能是错误的。而且,您没有使用表标记-对吗?