Mysql按用户获取最新文章和文章总数?

时间:2017-06-21 07:16:56

标签: mysql

似乎无法弄清楚如何获取用户发布的最新文章以及该用户发布的文章总数。

这是我尝试过的:

SELECT users.id, users.username, users.role, COUNT(articles.id) AS 
       total_articles, (SELECT articles.title 
       FROM articles ORDER BY id DESC LIMIT 1) AS latest_article_title
FROM users
JOIN articles ON users.id = articles.author_id

我有以下表格:

users (id, username, password, role) 
articles (title, body,created_at, author_id)

我只是在学习MYSQL,所以对此有任何帮助都会非常感激!

编辑: 一直在阅读你的答案,并想做我自己的查询,我理解并写自己,所以我提出了这个:

SELECT users.username, COUNT(articles.id) AS total_articles, 
       (SELECT articles.title FROM articles WHERE articles.author_id = users.id 
           ORDER BY articles.id DESC LIMIT 1) as latest_article_title
FROM users
LEFT JOIN articles ON users.id = articles.author_id
GROUP BY articles.author_id

可以吗?我的意思是,它有效,并且给了我确切的结果。但是,是“嵌套”选择,里面选择好用吗?

4 个答案:

答案 0 :(得分:1)

我不知道你的确切表结构,所以查询中可能有错,但我会尝试这样的事情:

 Select user.id, user.username, users.role, COUNT(articles.id) as total_articles, 
        (Select articles.title 
        from articles 
        Where author_id=user.id 
        order by articles.created_at desc Limit 1) as latest_article_title
from users
join articles ON user.id = articles.author_id

答案 1 :(得分:1)

试试这个

SELECT t.*,COUNT(articles.id) AS total_articles
FROM 
(
    SELECT a.title,a.id AS article_id,a.`author_id`,users.id AS user_id, 
           users.username, users.role
    FROM articles AS a
    INNER JOIN users ON a.author_id = users.id
    ORDER BY a.id DESC LIMIT 1
) t
INNER JOIN articles ON articles.`author_id` = t.author_id

希望它能解决你的问题

答案 2 :(得分:1)

您可以尝试低于sqls。我已经实施了所有和实施步骤以获得最终答案。

用户

id  username    password    role
1   mamta        123456 admin
2   hema         124378  user

文章

title   body            created_at  author_id
Poem    This is a poem  2017-06-01     1
Story   This is a Story 2017-06-20     1
Song    This is a Song  2017-06-10     2
Sangeet This is a Sangeet 2017-06-08    1
Rhyme   This is a Rhyme 2017-06-16      2

第1步

SELECT MAX( created_at ) AS created_at, COUNT( author_id ) AS Total
FROM  `articles` 
GROUP BY author_id

输出

created_at  Total
2017-06-20  3
2017-06-16  2

第2步

SELECT ar1.*,ar2.total FROM articles ar1
INNER JOIN  (
    SELECT MAX( created_at ) AS created_at, COUNT( author_id ) AS total
FROM  `articles` 
GROUP BY author_id
) ar2 
ON ar1.created_at = ar2.created_at

输出

title   body              created_at    author_id   total
Story   This is a Story   2017-06-20    1            3
Rhyme   This is a Rhyme   2017-06-16    2            2

第3步(最终的Sql)

SELECT u.id,u.username,u.role,ar.title ,ar.total AS total , ar.created_at FROM user u
INNER JOIN 
(
SELECT ar1.*,ar2.total FROM articles ar1
INNER JOIN  (
    SELECT MAX( created_at ) AS created_at, COUNT( author_id ) AS total
FROM  `articles` 
GROUP BY author_id
) ar2 
ON ar1.created_at = ar2.created_at
) ar ON ar.author_id = u.id

输出

id  username    role    title   total   created_at
1   mamta      admin    Story   3       2017-06-20
2   hema       user     Rhyme   2       2017-06-16

答案 3 :(得分:1)

您可以尝试使用此查询获取用户发布的最新文章ID和文章总数

SELECT articles.author_id, 
                         Count(articles.id) AS total_articles, 
                         Max(id)            latest_article_id 
                  FROM   articles 
                  GROUP  BY articles.author_id

然后,将此数据与用户文章表结合使用,以获得预期结果

SELECT u.id, 
       u.username, 
       u.role, 
       a.total_articles, 
       b.title AS latest_article_title 
FROM   users u 
       LEFT JOIN (SELECT articles.author_id, 
                         Count(articles.id) AS total_articles, 
                         Max(id)            latest_article_id 
                  FROM   articles 
                  GROUP  BY articles.author_id) a 
              ON u.id = a.author_id 
       LEFT JOIN articles b ON a.latest_article_id = b.id