如何统一3个不同的SELECT查询,这样它们可以用来从3个不同的类别中提取10个值,以便INSERT到一个临时表中?

问题描述 投票:1回答:2

有一个latest_comments表,每N分钟重写一次。内容从postscomments表中提取并合并在一起。有3个不同的类别(红色,绿色,蓝色),每个类别的10个最新评论应该在latest_comments中表示。

没有统一/合并查询看起来像:

INSERT INTO `latest_comments` (`post_id`, `comment_id`, `date`, `comment`, `category`)
SELECT
p.id AS `post_id`,
c.post_id AS `comment_id`,
c.date AS `date`,
c.comment AS `comment`,
p.category AS `category`
FROM `comments` AS c
LEFT JOIN `posts` AS p
ON c.post_id = p.id
WHERE p.category = 'red'
ORDER BY c.date DESC
LIMIT 0, 10;

INSERT INTO `latest_comments` (`post_id`, `comment_id`, `date`, `comment`, `category`)
SELECT
p.id AS `post_id`,
c.post_id AS `comment_id`,
c.date AS `date`,
c.comment AS `comment`,
p.category AS `category`
FROM `comments` AS c
LEFT JOIN `posts` AS p
ON c.post_id = p.id
WHERE p.category = 'green'
ORDER BY c.date DESC
LIMIT 0, 10;

INSERT INTO `latest_comments` (`post_id`, `comment_id`, `date`, `comment`, `category`)
SELECT
p.id AS `post_id`,
c.post_id AS `comment_id`,
c.date AS `date`,
c.comment AS `comment`,
p.category AS `category`
FROM `comments` AS c
LEFT JOIN `posts` AS p
ON c.post_id = p.id
WHERE p.category = 'blue'
ORDER BY c.date DESC
LIMIT 0, 10;

如何合并所有3个SELECT查询,因此只能使用1个INSERT查询,同时保持其在性能方面尽可能优化?

我已经尝试了所有我能找到的东西,并且我可以尝试提出我的逻辑。他们都没有工作,我认为3个单独的INSERT查询正在减慢功能。我希望的工作例如:

INSERT INTO `latest_comments` (`post_id`, `comment_id`, `date`, `comment`, `category`)
(SELECT
p.id AS `post_id`,
c.post_id AS `comment_id`,
c.date AS `date`,
c.comment AS `comment`,
p.category AS `category`
FROM `comments` AS c
LEFT JOIN `posts` AS p
ON c.post_id = p.id
WHERE p.category = 'red'
ORDER BY c.date DESC
LIMIT 0, 10),

(SELECT
p.id AS `post_id`,
c.post_id AS `comment_id`,
c.date AS `date`,
c.comment AS `comment`,
p.category AS `category`
FROM `comments` AS c
LEFT JOIN `posts` AS p
ON c.post_id = p.id
WHERE p.category = 'green'
ORDER BY c.date DESC
LIMIT 0, 10),

(SELECT
p.id AS `post_id`,
c.post_id AS `comment_id`,
c.date AS `date`,
c.comment AS `comment`,
p.category AS `category`
FROM `comments` AS c
LEFT JOIN `posts` AS p
ON c.post_id = p.id
WHERE p.category = 'blue'
ORDER BY c.date DESC
LIMIT 0, 10);
mysql sql mariadb sql-insert
2个回答
1
投票

使用UNION ALL

INSERT INTO `latest_comments` (`post_id`, `comment_id`, `date`, `comment`, `category`)

(SELECT
p.id AS `post_id`,
c.post_id AS `comment_id`,
c.date AS `date`,
c.comment AS `comment`,
p.category AS `category`
FROM `comments` AS c
LEFT JOIN `posts` AS p
ON c.post_id = p.id
WHERE p.category = 'red'
ORDER BY c.date DESC
LIMIT 0, 10)

UNION ALL 

(SELECT
p.id AS `post_id`,
c.post_id AS `comment_id`,
c.date AS `date`,
c.comment AS `comment`,
p.category AS `category`
FROM `comments` AS c
LEFT JOIN `posts` AS p
ON c.post_id = p.id
WHERE p.category = 'green'
ORDER BY c.date DESC
LIMIT 0, 10)

UNION ALL 

(SELECT
p.id AS `post_id`,
c.post_id AS `comment_id`,
c.date AS `date`,
c.comment AS `comment`,
p.category AS `category`
FROM `comments` AS c
LEFT JOIN `posts` AS p
ON c.post_id = p.id
WHERE p.category = 'blue'
ORDER BY c.date DESC
LIMIT 0, 10)

2
投票

如果你想从每个查询中获得10行,你可以使用UNION ALL

        INSERT INTO `latest_comments` (`post_id`, `comment_id`, `date`, `comment`, `category`)
        (SELECT
            p.id AS `post_id`,
            c.post_id AS `comment_id`,
            c.date AS `date`,
            c.comment AS `comment`,
            p.category AS `category`
        FROM `comments` AS c
        LEFT JOIN `posts` AS p ON c.post_id = p.id
        WHERE p.category = 'red'
        ORDER BY c.date DESC
        LIMIT 0, 10)
        UNION ALL
        (SELECT
            p.id AS `post_id`,
            c.post_id AS `comment_id`,
            c.date AS `date`,
            c.comment AS `comment`,
            p.category AS `category`
        FROM `comments` AS c
        LEFT JOIN `posts` AS p
        ON c.post_id = p.id WHERE p.category = 'green'
        ORDER BY c.date DESC LIMIT 0, 10 )
        UNION ALL
        (SELECT
            p.id AS `post_id`,
            c.post_id AS `comment_id`,
            c.date AS `date`,
            c.comment AS `comment`,
            p.category AS `category`
        FROM `comments` AS c
        LEFT JOIN `posts` AS p
        ON c.post_id = p.id WHERE p.category = 'blue'
        ORDER BY c.date DESC LIMIT 0, 10)

无论如何,你在where子句中使用左连接表列WHERE p.category = 'blue'这作为内连接

        INSERT INTO `latest_comments` (`post_id`, `comment_id`, `date`, `comment`, `category`)
        (SELECT
            p.id AS `post_id`,
            c.post_id AS `comment_id`,
            c.date AS `date`,
            c.comment AS `comment`,
            p.category AS `category`
        FROM `comments` AS c
        INNER JOIN `posts` AS p ON c.post_id = p.id and p.category = 'red'
        ORDER BY c.date DESC
        LIMIT 0, 10)

        UNION ALL
        (SELECT
            p.id AS `post_id`,
            c.post_id AS `comment_id`,
            c.date AS `date`,
            c.comment AS `comment`,
            p.category AS `category`
        FROM `comments` AS c
        INNER JOIN `posts` AS p ON c.post_id = p.id AND p.category = 'green'
        ORDER BY c.date DESC LIMIT 0, 10)
        UNION ALL
        (SELECT
            p.id AS `post_id`,
            c.post_id AS `comment_id`,
            c.date AS `date`,
            c.comment AS `comment`,
            p.category AS `category`
        FROM `comments` AS c
        INNER JOIN `posts` AS p ON c.post_id = p.id AND p.category = 'blue'
        ORDER BY c.date DESC LIMIT 0, 10);

或者如果您只需要30行,则可以使用IN子句

        INSERT INTO `latest_comments` (`post_id`, `comment_id`, `date`, `comment`, `category`)
        SELECT
            p.id AS `post_id`,
            c.post_id AS `comment_id`,
            c.date AS `date`,
            c.comment AS `comment`,
            p.category AS `category`
        FROM `comments` AS c
        INNER JOIN `posts` AS p ON c.post_id = p.id and p.category IN ( 'red', 'green', 'blue')
        ORDER BY c.date DESC
        LIMIT 0, 30;
© www.soinside.com 2019 - 2024. All rights reserved.