SQL JOIN计数和SUM问题

时间:2014-01-30 15:00:40

标签: mysql

我知道我错过了一些东西,我尝试了几种不同的方式,但我忽略了(或者太过努力)。有人可以告诉我这个SQL我错在哪里吗?

SELECT id, 
    COUNT(id) AS dupBlocks 
FROM tbl_duplicates8 INNER JOIN (
        tbl_accounts8, 
        tbl_delaccounts, 
        tbl_bad_bots, 
        tbl_log, 
        tbl_ipban, 
        tbl_ipban8
    ) ON (
        tbl_accounts8.SUM(num_attacks) AND 
        tbl_delaccounts.SUM(noattacks) AND 
        tbl_bad_bots.COUNT(id) AND 
        tbl_log.COUNT(id) AND 
        tbl_ipban.COUNT(txt_ip) AND 
        tbl_ipban8.COUNT(ip)
    );

我确实注意到了这个MySQL Join two tables count and sum from second table,但它在返回时给了我一个null。

任何帮助都将不胜感激。

为了更好地回答问题,这就是我目前正在做的事情:

$statsresults['newIPBan'] = $db->query("SELECT COUNT(ip) AS newIPBan FROM tbl_ipban8;");
$statsresults['oldIPBan'] = $db->query("SELECT COUNT(txt_ip) AS oldIPBan FROM tbl_ipban;");
$statsresults['log_blocks'] = $db->query("SELECT COUNT(id) AS logBlocks FROM tbl_log;");
$statsresults['badbots'] = $db->query("SELECT COUNT(id) AS badBots FROM tbl_bad_bots;");
$statsresults['del_num_attacks'] = $db->query("SELECT SUM(noattacks) AS deltotalattacks FROM tbl_delaccounts;");
$statsresults['num_attacks'] = $db->query("SELECT SUM(num_attacks) AS totalattacks FROM tbl_accounts8;");
$statsresults['dup_blocks'] = $db->query("SELECT COUNT(id) AS dupBlocks FROM tbl_duplicates8;");

将返回此内容:

|    ['newIPBan0newIPBan'] = String(6) "289033"
|    ['oldIPBan0oldIPBan'] = String(6) "125723"
|    ['log_blocks0logBlocks'] = String(4) "6481"
|    ['badbots0badBots'] = String(5) "15310"
|    ['del_num_attacks0deltotalattacks'] = String(9) "119494860"
|    ['num_attacks0totalattacks'] = String(8) "25286478"
|    ['dup_blocks0dupBlocks'] = String(6) "179916"

现在它正在调用数据库7次以获得每个总和或计数。我希望将其更改为1个数据库调用并返回它们的总和。

2 个答案:

答案 0 :(得分:2)

这是一种可以将它们组合在一起的方法:

select (newIPBan + oldIPBan + logBlocks + badBots + deltotalattacks + totalattacks + dupBlocks
       ) as NumIPs
from (SELECT COUNT(ip) AS oldIPBan FROM tbl_ipban8) ipb8 cross join
     (SELECT COUNT(txt_ip) AS newIPBan FROM tbl_ipban) ipb cross join
     (SELECT COUNT(id) AS logBlocks FROM tbl_log) l cross join
     (SELECT COUNT(id) AS badBots FROM tbl_bad_bots) bb cross join;
     (SELECT coalesce(SUM(noattacks), 0) AS deltotalattacks FROM tbl_delaccounts) da cross join
     (SELECT coalesce(SUM(num_attacks), 0) AS totalattacks FROM tbl_accounts8) ta cross join
     (SELECT COUNT(id) AS dupBlocks FROM tbl_duplicates8) d;

答案 1 :(得分:0)

修改

我会亲自为各种指标创建一个view,其行如下:

CREATE VIEW vwMetrics AS
    SELECT 'newIPBan' AS Metric, COUNT(ip) AS Value FROM tbl_ipban8
    UNION
    SELECT 'oldIPBan', COUNT(txt_ip) FROM tbl_ipban
    UNION
    SELECT 'logBlocks', COUNT(id) FROM tbl_log
    UNION
    SELECT 'badBots', COUNT(id) AS badBots FROM tbl_bad_bots
    UNION
    SELECT 'deltotalattacks', SUM(noattacks) FROM tbl_delaccounts
    UNION
    SELECT 'totalattacks', SUM(num_attacks) FROM tbl_accounts8
    UNION
    SELECT 'dupBlocks', COUNT(id) FROM tbl_duplicates8;

然后你可以聚合组件:

SELECT SUM(Value) AS TotalOfEverything
FROM vwMetrics;

该视图的好处是,您可以深入了解组件以进行详细/调试,而不是达到一个神奇的总体 - 可能会有一些重用视图在系统中的其他位置。

Prototype Fiddle here

(+感谢澄清问题)