鍍金池/ 問(wèn)答/Java  數(shù)據(jù)庫(kù)/ Mysql查詢問(wèn)題,根據(jù)最新發(fā)布文章或最新評(píng)論查詢出前幾名用戶信息。

Mysql查詢問(wèn)題,根據(jù)最新發(fā)布文章或最新評(píng)論查詢出前幾名用戶信息。

tb_user (用戶信息) id
tb_article(文章信息) id, 外鍵 user_id -> tb_user.id
tb_comment(評(píng)論信息) id, 外鍵 user_id -> tb_user.id
查詢最新的文章或評(píng)論的前5名用戶信息。
order by tb_article.create_time desc, tb_comment.create_time desc

問(wèn)題來(lái)了:這個(gè)SQL怎么寫不會(huì)有性能影響同時(shí)滿足需求?

回答
編輯回答
兮顏
//文章的  評(píng)論的改一下表就好了
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;
//一起?效率...就沒(méi)怎么考慮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
2018年8月26日 08:47