SQL Sum子查询与连接表?

时间:2013-09-18 03:59:48

标签: mysql sql subquery

我有一个论坛网站,帖子(宝石)和文件附件(gemdetail)以及帖子的回复(宝石),回复也可以有文件附件(gemdetail)。由于帖子和回复都存储在同一个表中,因此它会产生一个有趣的左连接,它选择所有具有相关回复和详细信息的帖子。

我想在混音(评级)中添加另一个表格,允许用户对每个帖子进行评分。然后,我希望能够在同一个查询中获得每个帖子的总评分。如何添加总和(评级),以便输出的每一行都有gemid的总和。我知道我需要一个sum子查询(派生表w / ad-hoc结果集),类似于找到的here,但它高于我的技能集。提前谢谢。

表结构如下

table: gems
gemid    title        replygemid
-----    -----        ----------
220      map              NULL
223      inhabitants      NULL
403      reply to map     220

table: gemdetail
gemid    filename
------   --------
220      uganda-map.jpg
220      mozambique-map.jpg
223      uganda-inhabitants.jpg
223      kenya-inhabitants.jpg
403      mona-lisa-x8.jpg 

table: rating (to be added)
gemid    rating
-----    -------
220       1
220       5
223       3
403      -1

我当前(简化)的查询

SELECT g.gemid as ggemid, g.title as gtitle, gemdetail.filename as gfilename, r.filename as rfilename
FROM (SELECT gems.* FROM gems ) g 
LEFT JOIN 
(SELECT title, x.gemid, x.replygemid, x.userid, y.filename  from gems x 
LEFT JOIN gemdetail y ON x.gemid = y.gemid ) r ON g.gemid = r.replygemid 
LEFT JOIN gemdetail ON g.gemid = gemdetail.gemid 

结果可能如下所示

ggemid   replygemid gtitle          gfilename                   rfilename
------   ---------- ------          ---------------------       ----------------
220      403        Map             uganda-map.jpg              mona-lisa-x8.jpg
220      403        Map             mozambique-map.jpg          mona-lisa-x8.jpg
223      NULL       Inhabitants     uganda-inhabitants.jpg      NULL
223      NULL       Inhabitants     kenya-inhabitants.jpg       NULL
223      NULL       Inhabitants     kenya-inhabitants.jpg       NULL

2 个答案:

答案 0 :(得分:1)

我认为这就是你想要的:

SELECT g.gemid as ggemid, g.title as gtitle, gemdetail.filename as gfilename, r.filename as rfilename, rt.sum_rating
FROM (SELECT gems.* FROM gems ) g 
LEFT JOIN 
(SELECT title, x.gemid, x.replygemid, x.userid, y.filename  from gems x 
LEFT JOIN gemdetail y ON x.gemid = y.gemid ) r ON g.gemid = r.replygemid 
LEFT JOIN gemdetail ON g.gemid = gemdetail.gemid 
LEFT JOIN (SELECT gemid, SUM(rating) as sum_rating from rating GROUP BY gemid) rt ON g.gemid = rt.gemid

答案 1 :(得分:1)

SQL Fiddle

<强>查询

SELECT g.gemid as ggemid, g2.gemid as replygemid, 
       g.title as gtitle, gd.filename as gfilename, 
       gd2.filename as rfilename, SUM(rating) as rating
FROM gems g
INNER JOIN gemdetail gd ON g.gemid = gd.gemid
INNER JOIN rating r ON g.gemid = r.gemid
LEFT OUTER JOIN gems g2 ON g.gemid = g2.replygemid
LEFT OUTER JOIN gemdetail gd2 ON g2.gemid = gd2.gemid
GROUP BY g.gemid, g2.gemid, g.title, 
         gd.filename, gd2.filename

<强> Results

| GGEMID | REPLYGEMID |       GTITLE |              GFILENAME |        RFILENAME | RATING |
|--------|------------|--------------|------------------------|------------------|--------|
|    220 |        403 |          map |     mozambique-map.jpg | mona-lisa-x8.jpg |      6 |
|    220 |        403 |          map |         uganda-map.jpg | mona-lisa-x8.jpg |      6 |
|    223 |     (null) |  inhabitants |  kenya-inhabitants.jpg |           (null) |      3 |
|    223 |     (null) |  inhabitants | uganda-inhabitants.jpg |           (null) |      3 |
|    403 |     (null) | reply to map |       mona-lisa-x8.jpg |           (null) |     -1 |