简体   繁体   English

带联接表的SQL Sum子查询?

[英]SQL Sum sub-query with joined tables?

I have a forum site with posts (gems) and file attachments (gemdetail) along with replies (gems) to the posts and the replies can also have file attachments (gemdetail). 我有一个论坛站点,其中包含帖子(gems)和文件附件(gemdetail)以及对帖子的回复(gems),并且回复中还可以包含文件附件(gemdetail)。 Since both posts and replies are stored in the same table, it makes for an interesting left join which selects all posts with associated replies and detail. 由于帖子和回复都存储在同一表中,因此可以进行有趣的左联接,该联接选择具有关联的回复和详细信息的所有帖子。

I want to add another table to the mix (rating) which allows the user to rate each post. 我想向混合(评分)添加另一个表,该表允许用户对每个帖子进行评分。 I then want to be able in the same query to get the sum total rating for each post. 然后,我希望能够在同一查询中获得每个帖子的总评分。 How to add sum(rating) so each row of the output will have the sum for the gemid. 如何将sum(rating)相加,以便输出的每一行都有gemid的总和。 I know I need a sum sub-query (derived table w/ ad-hoc result sets) similar to the one found here , but it is above my skill set. 我知道我需要一个总和子查询(带有临时结果集的派生表),类似于此处找到的子查询,但它超出了我的技能范围。 Thanks in advance. 提前致谢。

The table structure is as follows 表结构如下

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

My current (simplified) query 我当前的(简体)查询

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 

Results may look like this 结果可能如下所示

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

I think this is what you want: 我认为这是您想要的:

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

SQL Fiddle SQL小提琴

Query : 查询

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 |

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM