Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.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连接和_Mysql_Sql_Join_Group By_Sum - Fatal编程技术网

来自同一个表的mysql连接和

来自同一个表的mysql连接和,mysql,sql,join,group-by,sum,Mysql,Sql,Join,Group By,Sum,这是我的桌子: 我正试图列出收入最高的用户名单(排行榜)。 我使用的是SUM(收益)和用户名分组,但我也想SUM上传收益 在上表中,user1是test的上线,因此所有来自test的收入都应算作user1收入 这是我的代码: SELECT COUNT (a.id) as total, (SUM(CASE WHEN b.upline=a.username THEN b.up_earnings ELSE 0 END)+SUM(a.earnings)) as tot_ear

这是我的桌子:

我正试图列出收入最高的用户名单(排行榜)。 我使用的是SUM(收益)用户名分组,但我也想SUM上传收益

在上表中,user1test的上线,因此所有来自test的收入都应算作user1收入

这是我的代码:

SELECT COUNT (a.id) as total,
             (SUM(CASE WHEN b.upline=a.username THEN b.up_earnings ELSE 0 END)+SUM(a.earnings)) as tot_earnings,
             SUM(a.dls) as tot_dls,
             a.username
FROM logs a left join logs b ON a.username=b.upline
GROUP BY a.username ORDER BY tot_earnings DESC
但它不起作用!看起来表A的结果将重复:

欢迎任何帮助


谢谢

我认为在执行
连接之前,您需要通过
用户名
聚合表。如果我了解您试图做什么,这可能是一个问题:

SELECT l.username, count(*) as total_uplines,
       coalesce(sum(b.up_earnings), 0) + l.earnings as tot_earnings,
       l.tot_dls
FROM (select l.username, sum(l.earnings) as earnings,
             sum(l.dls) as tot_dls
      from logs l
      group by l.username
     ) l left join
     logs b
     ON l.username = b.upline
GROUP BY l.username, l.earnings, l.tot_dls
ORDER BY tot_earnings DESC;

图像?我们不是这样滚的,干得很好!谢谢有一个问题,如果我必须添加WHERE子句来检查日期是否为今天,我必须同时添加SELECT子句?@user3144996。可能如果您只想要从今天开始的
收入
增加收入
,那么您需要将过滤器添加到这两者中。