简体   繁体   English

将某些表与条件联接时,来自其他表的数据求和

[英]SUM data from other table when joining some tables with conditions

I got a complicated question. 我有一个复杂的问题。

I have a query that combine data from several tables. 我有一个查询,结合了来自多个表的数据。 for example: 例如:

traffic: id, traffic_source_id, first_hit_date
goals: id, goal_type, goal_date, goal_value
traffic_sources: id, source_name

goal_type could be "contact form", "demo download", "purchase" goal_value is numeric. Goal_type可以是“联系表格”,“演示下载”,“购买” Goal_value是数字。 and display the number of times the user reach the goal on the same date (day). 并显示用户在同一日期(天)达到目标的次数。

I'm trying to show one row for each traffic_id, and then sum every goal for this traffic_id. 我正在尝试为每个traffic_id显示一行,然后为该traffic_id汇总每个目标。 When a user comes for the first time, he/she gets a traffic id that stays with him (cookie). 当用户第一次来时,他/她会得到一个与他在一起的交通ID(cookie)。 he/she can reach a goal a week later after the first hit date, and then other goal 2 days later, for example. 例如,他/她可以在第一个匹配日期后一周后达到一个目标,然后在两天后达到另一个目标。

I want to be able to query from date > to date and show the correct sum values of the goals for that speific range. 我希望能够从date>到date进行查询,并显示该特定范围内目标的正确总和值。 When I try to use SUM(CASE WHEN ...) I can't sum just the range withing the goals table. 当我尝试使用SUM(CASE WHEN ...)时,我不能仅将范围与目标表相加。

example: traffic from: 1-feb to 28-feb 例如: 流量从1到2月

3 | google adwords | 0 contact | 1 demo download | 1 purchase
4 | facebook       | 1 contact | 3 demo download | 3 purchase

but when I want to change the range from 1-feb to 14-feb 但是当我想将范围从1feb更改为14feb时

3 | google adwords | 0 contact | 1 demo download | 0 purchase
4 | facebook       | 0 contact | 2 demo download | 2 purchase

hope I'm clear enough... 希望我足够清楚...

any advice will be much appreciated. 任何建议将不胜感激。


Update: query example of what I have now: 更新:查询我现在拥有的示例:

SELECT traffic.traffic_id as original_traffic_id hit_date, referrer, referrer_url, keyword, ip,
SUM(CASE goals.goal_type WHEN 'Contact' THEN 1 ELSE 0 END) goal_contact,
SUM(CASE goals.goal_type WHEN 'Download' THEN 1 ELSE 0 END) goal_download,
SUM(CASE goals.goal_type WHEN 'Signup' THEN 1 ELSE 0 END) goal_signup,
SUM(CASE goals.goal_type WHEN 'Purchase' THEN 1 ELSE 0 END) goal_purchase
FROM traffic
LEFT JOIN goals ON goals.traffic_id = traffic.traffic_id
WHERE traffic.traffic_id=100 AND hit_date >= '$from_date' AND hit_date <= '$to_date'

(where $from_date and $to_date are mysql date formats) (其中$ from_date和$ to_date是mysql日期格式)

(this is not the real query since the original query much larger and includes about 7 more tables that joins in) This query actually sums all the goals without having into cound goal_date. (这不是真正的查询,因为原始查询要大得多,并且包括大约7个加入的表)该查询实际上对所有目标进行求和,而无需输入球门Goal_date。 I want to be able to limit the SUM to the range of $from_date and $to_date 我希望能够将SUM限制在$ from_date和$ to_date的范围内

Hope it clears it a bit more. 希望它能清除更多。

I don't have the answer for this yet, but I what I did is to summerize all goals with another query prior to the first one. 我还没有答案,但是我要做的是在第一个查询之前用另一个查询总结所有目标。 The array looks like $goals_array[traffic_id][goal_type] -> value 数组看起来像$goals_array[traffic_id][goal_type] -> value

And while looping through the main sql, just fetch the value from the $goals_array. 在循环遍历主sql时,只需从$ goals_array中获取值即可。 It works fast, and altough I wish to make this on one query, that's ok for now. 它工作很快,并且完全可以在一个查询中进行,目前还可以。

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

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