还在学习MySQL,所以请耐心等待。
所以我有两个表:lead,即:
id dat in_click_id
55 2011-08-15 400
10 2011-08-13 400
34 2011-08-13 420
50 2011-08-12 420
15 2011-08-14 425
第二个表名为in_clicks:
id location keyword
400 KS word1
410 CO word2
415 LA word2
420 CA word2
425 FL word3
所以我试图找出一个潜在客户是否与点击相关联。 换句话说,我试图得到以下内容:
keyword count(keyword)
word1 15
... ...
所以关键字将是单词,第二列 将来自该关键字的潜在客户总数。
由于数据在两个单独的表中,我需要使用join命令,但是 我在实施它时遇到了一些问题。
这是我多次尝试中的一项,显然无效。
SELECT id, discriminator, create_date, in_click_id, COUNT(in_click_id)
FROM leads
WHERE create_date LIKE '%2011-08-17%'
ORDER BY COUNT(in_click_id) DESC
INNER JOIN in_click ON leads.in_click_id = in_clicks.id;
帮助!?
答案 0 :(得分:4)
您需要使用GROUP BY,如下所示:
SELECT leads.id, COUNT(in_click_id)
FROM leads
INNER JOIN in_click ON leads.in_click_id = in_clicks.id
WHERE create_date LIKE '%2011-08-17%'
GROUP BY leads.id
ORDER BY COUNT(in_click_id) DESC
答案 1 :(得分:1)
您的SQL子句被错误排序。典型的SQL查询采用以下格式
SELECT ...
FROM ...
{joins}
WHERE ...
GROUP BY ...
HAVING ...
ORDER BY ...
LIMIT ...
对于您的查询,您可能需要尝试以下内容,以计算与每个关键字相关联的潜在客户记录数。
SELECT c.keyword, count(l.id)
FROM in_click c
INNER JOIN leads l ON l.in_click_id = c.id
GROUP BY c.keyword
ORDER BY COUNT(l.id) DESC
答案 2 :(得分:1)
您需要使用Group By关键字:
SELECT in_clicks.keyword, COUNT(leads.in_click_id)
FROM in_clicks
INNER JOIN leads
ON leads.in_click_id = in_clicks.id
GROUP BY in_clicks.keyword
ORDER BY COUNT(leads.in_click_id)