内连接+计数+分组依据

时间:2012-09-26 18:17:19

标签: mysql count group-by inner-join

我无法计算/分组内部联接的结果

我有两张桌子

  1. results_dump:其中有两列:emailresult(结果值可以是openbounce

  2. all_data:其中有三列:emailfull_nameaddress

  3. 第一个目标是查询result_dump表,并对特定"open"的结果email进行计数和分组。

    此查询效果很好:

    SELECT `email`, COUNT(*) AS count
    FROM `result_dump`
    WHERE `date` = "open" 
    GROUP BY `email`
    HAVING COUNT(*) > 3
    ORDER BY count DESC
    

    它取得这些结果的第二个目标(任何“打开”超过3次的人)并提取“full_name”和“地址”,这样我就会详细了解谁开了3次以上的电子邮件。

    我有这个查询,它可以将数据汇总到一起 - 但我无法弄清楚如何让COUNTHAVINGORDERINNER JOIN

    SELECT *
    FROM all_data
    INNER JOIN result_dump ON 
    all_data.email = result_dump.email
    where `result` = "open" 
    

3 个答案:

答案 0 :(得分:4)

SELECT email,name,count(*)
FROM all_data
INNER JOIN result_dump ON 
all_data.email = result_dump.email
where `result` = "open" 
group by result_dump.email
having count(*)>3
ORDER by count DESC

我认为这个没有错。

答案 1 :(得分:1)

尝试以下查询:

SELECT * FROM all_data AS a
INNER JOIN 
(SELECT * FROM result_dump where email IN 
                                    (SELECT `email`
                                     FROM `result_dump`
                                     WHERE `date` = "open" 
                                     GROUP BY `email`
                                     HAVING count(email) >3
                                     ORDER BY count(email) DESC)) AS b
ON a.email = b.email
WHERE b.`result` = "open" 

答案 2 :(得分:-1)

这很好...... 尝试这个..

SELECT title.title,count(*),title.production_year,title.id as movie_id,title.flag as language,movie_info.info from title INNER JOIN movie_info ON title.id = movie_info.movi​​e_id;