Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用分组统计MySQL中对自己ID的引用_Mysql_Sql_Select - Fatal编程技术网

使用分组统计MySQL中对自己ID的引用

使用分组统计MySQL中对自己ID的引用,mysql,sql,select,Mysql,Sql,Select,我正在数据库中对帖子进行查询。如果帖子是对另一篇帖子的响应,那么它的parent\u id大于零,否则为零 为了生成关于帖子的报告,我在SELECT中使用以下命令,然后按用户分组 SELECT SUM(IF(parent_id = 0, 1, 0)) as 'NewPosts', SUM(IF(parent_id > 0, 1,0)) as 'Responses', COUNT(parent_id) as 'TotalPosts', FRO

我正在数据库中对帖子进行查询。如果帖子是对另一篇帖子的响应,那么它的
parent\u id
大于零,否则为零

为了生成关于帖子的报告,我在SELECT中使用以下命令,然后按用户分组

SELECT
    SUM(IF(parent_id = 0, 1, 0)) as 'NewPosts',
    SUM(IF(parent_id > 0, 1,0))  as 'Responses',
    COUNT(parent_id)             as 'TotalPosts',
FROM posts
GROUP BY user
现在我需要添加一个列,显示用户的自我反应。类似于

SUM(IF(parent_id IN id, 1, 0)) as 'SelfResponses'
当然,我知道这是错误的,但我希望这能传达出我的想法

编辑:数据如下所示:

User        Id        parent_id
Henry       12          0
Henry       24          12
Henry       32          16
Joseph      16          0
因此,在这种情况下,输出为:

User         NewPosts        Responses     TotalPosts        SelfResponses
Henry          2                2             3                    1
Joseph         1                0             1                    0

假设响应的parentId是响应的postId,那么可以通过以下方式实现

查询1

SELECT
   a.user,
   SUM(IF(a.parent_id = 0, 1, 0)) as 'NewPosts',
   SUM(IF(a.parent_id > 0, 1,0))  as 'Responses',
   COUNT(a.parent_id)             as 'TotalPosts',
   SUM(IF(a.user = b.user, 1, 0)) as 'SelfResponses'
FROM 
  Table1 a
LEFT JOIN
  Table1 b
ON 
  a.parent_id = b.id
GROUP BY 
  a.user
|   USER | NEWPOSTS | RESPONSES | TOTALPOSTS | SELFRESPONSES |
--------------------------------------------------------------
|  Henry |        1 |         2 |          3 |             1 |
| Joseph |        1 |         0 |          1 |             0 |
结果

SELECT
   a.user,
   SUM(IF(a.parent_id = 0, 1, 0)) as 'NewPosts',
   SUM(IF(a.parent_id > 0, 1,0))  as 'Responses',
   COUNT(a.parent_id)             as 'TotalPosts',
   SUM(IF(a.user = b.user, 1, 0)) as 'SelfResponses'
FROM 
  Table1 a
LEFT JOIN
  Table1 b
ON 
  a.parent_id = b.id
GROUP BY 
  a.user
|   USER | NEWPOSTS | RESPONSES | TOTALPOSTS | SELFRESPONSES |
--------------------------------------------------------------
|  Henry |        1 |         2 |          3 |             1 |
| Joseph |        1 |         0 |          1 |             0 |

希望这有助于调整您的表格结构。您需要一个自联接来查找帖子的响应。请在此表中发布一些示例数据
posts
和所需的输出。它不应该是
2
new post for Henry,它应该是
1
。您还可以告诉我进行
求和(如果(SELECT id中的parent_id来自于…
或自联接的帖子?这不应该是一个
左联接吗?
?我宁愿选择联接,而不是内部查询,因为对于每个有效记录,将执行内部查询,因此从性能的末尾看,这将更加昂贵。如果您同时检查这两个查询,效果会更好。)检查你的机器,看哪一个更好。