SQL - 检索用户发布的帖子数量

时间:2017-04-15 07:11:06

标签: mysql sql

我有一个名为test_opplegg的表格,上面有帖子。一列是test_opplegg.UserId。现在,当我打印出每个帖子时,我还想制作一个变量NumOpp来计算这篇帖子的用户/作者共发布的数量。

$sql = $conn->prepare("SELECT test_opplegg.Title, test_opplegg.id as oid, test_opplegg.Desc, test_opplegg.ShortDesc,test_opplegg.Type, test_opplegg.Approved, test_opplegg.Language, 
                        test_opplegg.Grade, test_opplegg.UserId, test_opplegg.Subject, test_opplegg.Link, test_users.id,  test_users.user_Username,  test_users.user_Name,
                        DATE_FORMAT(test_opplegg.TimeAdded,GET_FORMAT(DATE,'EUR')) as TimeAdded,
                        SUM(test_thumbs.IntValue) AS SumThumbs, 
                        SUM(CASE WHEN test_thumbs.OId = test_opplegg.Id THEN 1 ELSE 0 END) AS NumThumbs,
                        SUM(CASE WHEN test_opplegg.UserId = test_opplegg.UserId THEN 1 ELSE 0 END) AS NumOpp

                        FROM `test_opplegg` 
                        INNER JOIN test_users ON test_opplegg.UserId = test_users.id 
                        LEFT JOIN test_thumbs ON test_thumbs.OId = test_opplegg.Id GROUP BY test_opplegg.Id
                        ORDER BY id desc;");

是否可以编写一个SQL语句来计算一个作者在同一个表中的帖子数量?

以下是该表的简化版本:

| id | Title | Desc | UserId |
|  1 | ....  | .... |   3    |
|  2 | ....  | .... |   3    |
|  3 | ....  | .... |   2    |
|  4 | ....  | .... |   2    |
|  5 | ....  | .... |   3    |
|  6 | ....  | .... |   5    |
|  7 | ....  | .... |   4    |

这里我要打印所有.id,但是对于每个.id我打印我还要打印UserId总共发布了多少帖子。当我打印帖子(id = 1)时,我想得到UserId(3)共有3个帖子。 当我打印帖子(id = 7)时,我想获得User(UserId 4)发布的帖子总数,而UserId = 4只发了1个帖子。那么它应该打印1。

我可以获得作者发布的帖子总数吗?

2 个答案:

答案 0 :(得分:0)

请尝试以下方法......

SELECT test_opplegg.Title,
       test_opplegg.id as oid,
       test_opplegg.Desc,
       test_opplegg.ShortDesc,
       test_opplegg.Type,
       test_opplegg.Approved,
       test_opplegg.Language,
       test_opplegg.Grade,
       test_opplegg.UserId,
       test_opplegg.Subject,
       test_opplegg.Link,
       test_users.id,
       test_users.user_Username,
       test_users.user_Name,
       DATE_FORMAT( test_opplegg.TimeAdded,
                    GET_FORMAT( DATE,
                                'EUR' ) ) AS TimeAdded,
       SUM( test_thumbs.IntValue ) AS SumThumbs,
       SUM( CASE
                WHEN test_thumbs.OId = test_opplegg.Id THEN
                    1
                ELSE
                    0
            END ) AS NumThumbs,
       countOfUserIDFinder.countOfUserID AS NumOpp
FROM test_opplegg
JOIN test_users ON test_opplegg.UserId = test_users.id
LEFT JOIN test_thumbs ON test_thumbs.OId = test_opplegg.Id
JOIN ( SELECT UserId
              COUNT( UserId ) AS countOfUserID
       FROM test_opplegg
       GROUP BY UserId
     ) AS countOfUserIDFinder ON test_opplegg.UserId = countOfUserIDFinder.UserId
GROUP BY test_opplegg.Id
ORDER BY id desc;

此声明以您的问题中的一个为基础,通过在以下细分生成的表格中添加INNER JOIN ...

SELECT UserId
       COUNT( UserId ) AS countOfUserID
FROM test_opplegg
GROUP BY UserId

此细分会生成每个UserId的列表及其相关的帖子数。将其加入到您的代码中会产生向每个帖子的数据添加字段的效果,其中包含与该帖子相关联的UserId的帖子数。选中此值后,将调用NumOpp。您之前用于计算NumOpp的细分已被替换。

如果您对评论有任何疑问,请随时发表评论。

答案 1 :(得分:0)

试试这个:

$sql = $conn->prepare("SELECT test_opplegg.Title, test_opplegg.id as oid, test_opplegg.Desc, test_opplegg.ShortDesc,test_opplegg.Type, test_opplegg.Approved, test_opplegg.Language, 
                        test_opplegg.Grade, test_opplegg.UserId as uidaa, test_opplegg.Subject, test_opplegg.Link, test_users.id,  test_users.user_Username,  test_users.user_Name,
                        DATE_FORMAT(test_opplegg.TimeAdded,GET_FORMAT(DATE,'EUR')) as TimeAdded,
                        SUM(test_thumbs.IntValue) AS SumThumbs, 
                        SUM(CASE WHEN test_thumbs.OId = test_opplegg.Id THEN 1 ELSE 0 END) AS NumThumbs,

                        (SELECT COUNT(test_opplegg.UserId) FROM test_opplegg WHERE test_opplegg.UserId = uidaa) AS NumOpp
                        FROM `test_opplegg` 
                        INNER JOIN test_users ON test_opplegg.UserId = test_users.id 
                        LEFT JOIN test_thumbs ON test_thumbs.OId = test_opplegg.Id GROUP BY test_opplegg.Id, test_opplegg.UserId
                        ORDER BY id desc;");