简体   繁体   English

mysql报表联接表和数据库结构问题

[英]mysql report join tables and Database structure issue

I'm trying to achieve the following report result: 我正在尝试实现以下报告结果:

campaign_name   visits  leads   registrations
3333              3       0         0
direct            3       2         1
(null)            0       1         1
test              0       1         1

I have this database structure and I'm quite sure now that the relationships are incorrect. 我有这个数据库结构,现在我很确定关系是不正确的。 I played around with joining them but I am doing it wrong by http://sqlfiddle.com/#!9/451de/7 我和他们一起玩耍,但http://sqlfiddle.com/#!9/451de/7做错了

Your advise would be appreciated. 您的建议将不胜感激。

Try this 尝试这个

mysql_query("select  campaign_name  , count(t.id) 
             as visits, count(l.id) as leads ,count(r.id) as registrations

from trackings t

left join leads as l
 on l.tracking_id =t.id

left join registrations as r
 on r.tracking_id =t.id

where t.created_at between '2015-04-01' AND '2015-05-31' and t.aff_id =5

group by campaign_name");

Try This. 尝试这个。

SELECT `campaign_name`, 
   count(t.id) as visits, 
   count(l.id) as leads ,
   count(r.id) as registrations

FROM `trackings` t

   LEFT JOIN leads l ON t.`id` = l.`tracking_id` 
   LEFT JOIN registrations r ON t.`id` = r.`tracking_id`

WHERE t.created_at between '2015-04-01'
      AND '2015-05-31' 
      AND t.aff_id =5

GROUP BY `campaign_name`

ORDER BY visits DESC

not what you are looking for because of date issue. 不是因为日期问题而在寻找什么。 trackings table id 跟踪表ID

6           2015-04-07 18:54:57
26          2015-04-13 11:06:13
50          2015-04-20 14:42:23

while leads 当线索

26         2015-05-13 11:07:09
50         2015-05-20 14:43:07

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM