开发者

Joining together five tables

Allright, I am stuck on a problem.

I have about 5 database tables. One table has a list of campaigns, another has a list of ad groups, another has a list of ads, another has a list of in clicks, another has a list of leads, and another has a list of lead status. Each table also has other columns. For example, in clicks has an ad_id and column account, and the lead status table has a lead id, buyer, and amount. Each table a column with an id associated with another table id.

So I'm trying to get my data into the following format

campaign   keyword   count(keyword)   sum(amount)   buyer
bing-auto  rap        15                45           david
google-auto honda     10                30           chris

Basically, it's every successful lead grouped by keyword and campaign.

However, I just can't seem to solve th开发者_高级运维is problem.

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

I know this is not a good explanation, and my above query is not good, but I'm a little stuck on how to proceed. Can anyone help point me in the right direction.

EDIT: Here's the table schema.

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


Something like this?

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

Remarks
Don't use LIKE for datefields, use BETWEEN startdate AND enddate.
If you use LIKE %.... you will not be able to use an index.
Try and use LIKE cghxgs%, that way you will be able to use an index.
If you need to do index text matches, use full text indexes with the MATCH AGAINST syntax.
This will only work on MyISAM tables however....

Links:
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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜