好吧,我遇到了问题。
我有大约5个数据库表。一个表格包含广告系列列表,另一个表格包含广告组列表,另一个表格包含广告列表,另一个表格包含点击次数列表,另一个表格包含潜在客户列表,另一个表格包含潜在客户状态列表。 每个表还有其他列。例如,点击中包含ad_id和列帐户,并且潜在客户状态表具有潜在客户ID,买方和金额。每个表都有一个id与另一个表id相关联的列。
所以我试图将我的数据变成以下格式
campaign keyword count(keyword) sum(amount) buyer
bing-auto rap 15 45 david
google-auto honda 10 30 chris
基本上,它是按关键字和广告系列分组的每个成功线索。
但是,我似乎无法解决这个问题。
SELECT keyword, COUNT(keyword) FROM in_clicks AS ic
INNER JOIN ads AS a ON ic.ad_id = a.id
INNER JOIN ad_groups AS ag ON a.ad_group_id = ag.id
INNER JOIN campaigns AS c ON ag.campaign_id = c.id;
INNER JOIN lead_status AS ls ON l.id = ls.id
INNER JOIN leads AS l ON ic.in_click_id = l.id
WHERE ic.create_date LIKE '%2011-08-19%' AND ic.location NOT LIKE '%Littleton%'
GROUP BY ic.keyword ORDER BY COUNT(ic.keyword) DESC
我知道这不是一个好的解释,我上面的查询并不好,但我有点坚持如何继续。任何人都可以帮助我指出正确的方向。
编辑: 这是表格架构。
CAMPAIGN
id name trafficsource createdate
20 hip hop bing 2001-08-16
21 rap bing 2001-08-18
AD GROUPS
id name campaignid createdate
15 jayz 20 2001-08-16
16 eminem 21 2001-08-18
ADS
id headline adgroupid
13 cool is cool 15
14 wow, great 16
IN CLICKS
id keyword adid createdate
205 his name 13 2001-08-16
206 whoa 14 2001-08-18
LEADS
id in_click_id create_date
300 205 2001-08-16
301 206 2001-08-18
LEAD STATUS
id lead_id success amount buyer
501 300 0 5 jack
502 300 1 15 jill
答案 0 :(得分:2)
这样的东西?
SELECT
c.name as campaign
, ic.keyword
, count(ic.keyword) as keycount
, sum(ls.amount) as total_amount
, group_concat(ls.buyer) as buyers
FROM in_click ic
INNER JOIN ads AS a ON (ic.ad_id = a.id)
INNER JOIN ad_groups AS ag ON (a.ad_group_id = ag.id)
INNER JOIN campaigns AS c ON (ag.campaign_id = c.id)
INNER JOIN lead_status AS ls ON (l.id = ls.id)
INNER JOIN leads AS l ON (ic.in_click_id = l.id)
WHERE ic.create_date BETWEEN 2011-08-01 and 2011-08-31
AND ic.location NOT LIKE 'test%'
GROUP BY ic.keyword
ORDER BY keycount
<强>说明强>
不要将LIKE
用于日期字段,请使用BETWEEN startdate AND enddate
如果您使用LIKE %....
,则无法使用索引
尝试并使用LIKE cghxgs%
,这样您就可以使用索引
如果需要进行索引文本匹配,请使用带有MATCH AGAINST
语法的全文索引
这只适用于MyISAM表....
<强>链接:强>
http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#operator_between
http://dev.mysql.com/doc/refman/5.5/en/fulltext-search.html#function_match
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat