1

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

我想在组合(评分)中添加另一个表,允许用户对每个帖子进行评分。然后,我希望能够在同一个查询中获得每个帖子的总评分。如何添加 sum(rating),以便输出的每一行都有 gemid 的总和。我知道我需要一个类似于此处找到的总和子查询(带有临时结果集的派生表),但它超出了我的技能。提前致谢。

表结构如下

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
4

2 回答 2

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
于 2013-09-18T04:37:26.423 回答
1

SQL小提琴

查询

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

结果

| 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 |
于 2013-09-18T04:43:53.260 回答