mysql查询使用4个表来生成小报表

时间:2015-08-27 18:42:32

标签: mysql

我无法让查询工作。此查询中使用了4个表。我想要一份看起来像这样的报告......

Name Success Failure jdn 5 1 wer 0 3

我想生成过去24小时的数据。这是我尝试过但没有用的示例查询。

select r.name as jid,
 i.id as theid,
 (select sum(counter) as scount 
  from WebServicesIfSummarySuccess 
  where id=theid 
    and dtime between date_sub(now(),interval 24 hour) and now() 
    and sum(counter) >0 and sum(counter) is not null 
  group by id
 ) as success,
 (select sum(counter) as fcount 
  from WebServicesIfSummaryFailure 
  where id=theid 
    and dtime between date_sub(now(),interval 24 hour) and now() 
    and sum(counter) >0 and sum(counter) is not null 
  group by id
 ) as failure
 from router r, interface i 
 where r.rid = i.rid  
 and r.name like '%wi%' ; 

以下是表格布局。

router +-------+---------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+---------------------+------+-----+---------+----------------+ | rid | int(11) unsigned | NO | PRI | NULL | auto_increment | | name | char(120) | NO | MUL | | | | pop | char(10) | NO | MUL | | | | popid | tinyint(3) unsigned | NO | | 0 | | +-------+---------------------+------+-----+---------+----------------+ interface +-------------+---------------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------------+---------------------------+------+-----+---------+----------------+ | id | int(11) unsigned | NO | PRI | NULL | auto_increment | | name | char(255) | NO | MUL | | | | rid | int(11) | NO | MUL | 0 | | | speed | bigint(11) | YES | | NULL | | | description | char(255) | YES | | NULL | | | status | enum('active','inactive') | YES | | active | | +-------------+---------------------------+------+-----+---------+----------------+ Failure and Success are 2 separate tables +---------+------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+------------------+------+-----+---------+-------+ | id | int(10) unsigned | NO | PRI | NULL | | | dtime | datetime | NO | PRI | NULL | | | counter | bigint(20) | NO | | NULL | | | rate | int(11) | YES | | NULL | | +---------+------------------+------+-----+---------+-------+

0 个答案:

没有答案