Mysql query questions, according to the latest published articles or the latest comments to query the information of the first few users.

tb_user (user information) id
tb_article (article information) id, foreign key user_id-> tb_user.id
tb_comment (comment information) id, foreign key user_id-> tb_user.id
query the top 5 users of the latest article or comment.
order by tb_article.create_time desc, tb_comment.create_time desc

The

question arises: how do you write this SQL without having a performance impact and meeting the requirements at the same time?

Feb.28,2021

//  
select tb_user.* from tb_article join tb_user on tb_user.id=tb_article.id order by tb_article.create_time desc limit 5;
//?...233.....
SELECT
    a.uid,
    username
FROM
    (
        (
            SELECT
                `uid`,
                `create_time` AS `time`
            FROM
                tb_article
        )
        UNION ALL
            (
                SELECT
                    `uid`,
                    `create_time` AS time
                FROM
                    tb_comment
            )
    ) AS a
JOIN tb_userAS b ON a.uid = b.uid
GROUP BY
    a.uid
ORDER BY
    a.time DESC
LIMIT 5
Menu