MySQL加入三个表,计数并获取最后一个用户名

时间:2015-05-23 13:08:51

标签: mysql sql join count

我的SQL查询有问题。我需要连接三个表,计算行,获取最后一行和创建自定义列。 我的表格如下所示:

表名:文章

_______________________________________
idarticle     idwriter     title
---------------------------------------
1             1            Title One
2             3            Title Two
3             2            Title Three

表名:评论

________________________________________________________________________________
idcomment     idarticle     idcommented     content          datetime
--------------------------------------------------------------------------------
1             1             2               Comment One      2015-05-15 00:00:00
2             1             3               Comment Two      2015-05-16 00:00:00
3             1             1               Comment Three    2015-05-17 00:00:00
4             2             2               Comment Four     2015-05-18 00:00:00
5             3             3               Comment Five     2015-05-19 00:00:00
6             3             2               Comment Six      2015-05-20 00:00:00

表名:成员

_____________________
idmember     username
---------------------
1            apple
2            orange
3            banana

如何使用一个查询加入所有表,计数并获取最后一个注释+用户名。 可能结果如下:

_____________________________________________________________________________________________________________________
idarticle idwriter title       username_writer totalcomments lastcomment_id lastcomment_username lastcomment_datetime
---------------------------------------------------------------------------------------------------------------------
3         2        Title Three orange          2             2              orange               2015-05-20 00:00:00
2         3        Title Two   banana          1             2              orange               2015-05-18 00:00:00
1         1        Title One   apple           3             1              apple                2015-05-17 00:00:00

我希望有人能解决我的问题。 我使用PHP 5.4,MySQL 5.5和MeekroDB库,因此查询必须支持这些。 对不起我的英语不好。 谢谢

2 个答案:

答案 0 :(得分:0)

http://sqlfiddle.com/#!9/132336/1

SELECT a.idarticle, a.idwriter , a.title, 
       m.username, 
       c.idcomment, cm.username, c.datetime
FROM article a
LEFT JOIN member m
ON a.idwriter = m.idmember
LEFT JOIN comment c
ON a.idarticle = c.idarticle
LEFT JOIN comment c1
ON a.idarticle = c1.idarticle
  AND c.datetime<c1.datetime
LEFT JOIN member cm
ON c.idcommented = cm.idmember
WHERE c1.datetime IS NULL

答案 1 :(得分:-1)

试试这个......

SELECT 
    a.idarticle, 
    a.idwriter, 
    a.title,
    (SELECT COUNT(*) FROM comment c WHERE c.idarticle = a.idarticle) AS totalcomments,
    (SELECT c2.idcommented FROM comment c2 WHERE c2.idarticle = a.idarticle) AS lastcomment_id,
    (SELECT m.username FROM comment c3 INNER JOIN member m ON c3.idcommented = m.idmember WHERE c3..idarticle = a.idarticle ORDER BY c3.datetime DESC LIMIT 1) AS lastcomment_username,
    (SELECT c4.datetime FROM comment c4 INNER JOIN member m2 ON c4.idcommented = m2.idmember WHERE c4..idarticle = a.idarticle ORDER BY c4.datetime DESC LIMIT 1) AS lastcomment_datetime
    FROM article a