简体   繁体   English

结合这三个SQL查询

[英]Combining these three SQL Queries

I have developed these three queries, found below, that find the most views, most comments, and most likes that each Article has (an article is defined by both the Content ID and Format). 我已经开发了以下三个查询,可以找到每个文章的最多视图,最多评论和最喜欢(文章由Content ID和Format定义)。 I undersand that Target ID != ContentID and that Format != TargetClass, but I treat them as they are the same thing. 我理解了Target ID!= ContentID和Format!= TargetClass,但是我将它们视为同一对象。

I need to use these three queries to output the top 3 articles that hold the most comments, likes and views, in priority of that order. 我需要使用这三个查询来输出排名靠前的3条评论,喜欢和观看次数最多的文章。 I'm not exceptionally talented at SQL, so could someone please provide a solution and a mild explanation? 我在SQL方面并不是特别有才华,所以有人可以提供解决方案和温和的解释吗? If the question needs to be rephrased, please make it clear and I will do so. 如果问题需要改写,请说明清楚,我会这样做。 Cheers. 干杯。

MOST VIEWS (In descending order; with most views being at the top) 大多数视图 (以降序;与大多数观点是在顶部)

SELECT ContentID, Format
FROM Content
GROUP BY ContentID, Format
ORDER BY COUNT(View) DESC

MOST COMMENTS (In descending order; with most comments being at the top) 最多评论 (按降序排列;大多数评论在顶部)

SELECT TargetID, TargetClass
FROM Comments
GROUP BY TargetID, TargetClass
ORDER BY COUNT(TargetID) DESC

MOST LIKES (In descending order; with most likes being at the top) 最喜欢 (按降序排列;大多数喜欢位于顶部)

SELECT ContentID, Format
FROM Likes
GROUP BY ContentID, Format
ORDER BY COUNT(ContentID) DESC

EXAMPLE DATA AND OUTPUT (As requested) TABLE 1: 数据和输出示例 (按要求) 表1:

ContentID| Format  |View|
---------|---------|----|
    1    |Paperback|1700|
---------|---------|----|
    1    |  Ebook  |1500|
---------|---------|----|
    2    |Paperback|1500|
-------------------------

TABLE 2: 表2:

CommentID|TargetID |TargetClass|
---------|---------|-----------|
    1    |    1    |   Ebook   |
---------|---------|-----------|
    2    |    2    | Paperback |
---------|---------|-----------|
    3    |    1    |   Ebook   |
--------------------------------

TABLE 3: 表3:

  LikeID | ContentID| Format  |
---------|---------|---------|
    1    |    1    |Ebook    |
---------|---------|---------|
    2    |    2    |Paperback|
---------|---------|---------|
    3    |    2    |Paperback|
------------------------------

DESIRED SOLUTION: 所需解决方案:

In table 1 contentId = 1 and format = paperback have the most views, BUT, views are weigh less than likes when decided the popularity of an article. 在表1中,contentId = 1和format =平装书拥有最多的观看次数,但是,在决定文章的人气时,观看的权重小于喜欢的程度。 In table 3, ContentId=2 and format=paperback, have more likes than either of the other two articles (the one absent from the table has no likes at all). 在表3中,ContentId = 2和format =平装本的喜好度超过其他两篇文章中的任何一篇(表中缺少的一篇根本没有喜好)。 Comments are weighted most heavily however, and in table 2, targetid=1, format=ebook has more comments than either of the other two articles. 但是,注释的权重最大,在表2中,targetid = 1,format = ebook的注释比其他两篇文章中的任何一篇都多。

This means that targetid=1 and format=ebook is the most popular book. 这意味着targetid = 1和format = ebook是最受欢迎的书。 HOWEVER, when two have the same number of comments, they fall back on the likes to determine the most popular article. 但是,当两个评论的数量相同时,它们会根据喜欢的程度来确定最受欢迎的文章。 However, if, once again, they have the same number of likes, it falls back on views to determine the most popular article. 但是,如果他们再次具有相同的点赞次数,则只能依靠视图来确定最受欢迎的文章。

The output required is a list as follows: 所需的输出是一个列表,如下所示:

ContentID | Format
1         | ebook
2         | paperback
1         | paperback

In order of their "popularity". 为了他们的“受欢迎程度”。

I think you can do this with union or union all . 我认为您可以通过unionunion all来做到这一点。 Perhaps the following does what you want: 也许以下是您想要的:

(SELECT TargetID as ContentId, TargetClass
 FROM Comments
 GROUP BY TargetID, TargetClass
 ORDER COUNT(*) DESC
 LIMIT 1
)
UNION ALL
(SELECT ContentID, Format
 FROM CONTENT
 GROUP BY ContentID, Format
 ORDER BY COUNT(*) DESC
 LIMIT 1
)
UNION ALL
(SELECT ContentID, Format
 FROM Likes
 GROUP BY ContentID, Format
 ORDER BY COUNT(*) DESC
 LIMIT 1
)

I think this is the correct solution: 我认为这是正确的解决方案:

SELECT V1.ContentID, V1.Format
FROM 
        ((SELECT ContentID, Format ,Sum(View) AS CountView
        FROM Content
        GROUP BY ContentID, Format) V1) LEFT OUTER JOIN 

        (
          SELECT V2.ContentID, V2.Format ,CountComments,CountLikes
          FROM
          ((SELECT TargetID AS ContentID, TargetClass AS Format,Count(TargetID) AS CountComments
          FROM Comments
          GROUP BY TargetID, TargetClass) V2) LEFT OUTER JOIN 

          ((SELECT ContentID, Format ,Count(ContentID) AS CountLikes
          FROM Likes
          GROUP BY ContentID, Format) V3)

          ON(V3.ContentID=V2.ContentID AND V3.Format =V2.Format)
        ) V12
        ON(V1.ContentID=V12.ContentID AND V1.Format =V12.Format)
ORDER BY CountComments DESC,CountLikes DESC ,CountView DESC
LIMIT 3;

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

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