1

我有一个名为 td_cell_allocation 的表,其中包含以下数据:

td_cell_allocation

|-------------|-----------|------------------------------------|---------------|
| first_name  | last_name |  user_email      |  cell_image     | continent     |
|--------------------------------------------------------------|----------------
| saswat      | roy       | sas@gmail.com    |   a.jpg         |  america      |
--------------------------------------------------------------------------------
| shuvra      | pan       |   s@gmail.com    |   b.jpg         |  europe       |
--------------------------------------------------------------------------------


td_cell_click

|-------------|-----------------|------------------|
| click_id    | user_mail       |  date_of_click   |
|--------------------------------------------------|
|     1       | sas@gmail.com   | 2013-06-2        |
----------------------------------------------------
|     2       | s@gmail.com     | 2013-06-2        |
----------------------------------------------------
|     3       | s@gmail.com     | 2013-06-3        |
----------------------------------------------------
|     4       | sas@gmail.com   | 2013-06-4        |
----------------------------------------------------
|     5       | s@gmail.com     | 2013-06-5        |
----------------------------------------------------
|     6       | sas@gmail.com   | 2013-06-5        |
----------------------------------------------------
|     7       | sas@gmail.com   | 2013-06-5        |
----------------------------------------------------
|     8       | sas@gmail.com   | 2013-06-8        |
----------------------------------------------------
|     9       | s@gmail.com     | 2013-06-8        |
----------------------------------------------------

现在我想按大洲对 6 月份点击次数最多的用户进行排序

所以我写了一个这样的查询:

SELECT first_name, last_name, count( click_id ) AS tot_click, td_cell_allocation.continent
                        FROM td_cell_allocation, td_cell_click
                        WHERE td_cell_allocation.user_email = td_cell_click.user_mail
                        AND date_of_click LIKE '%-06-%'
                        GROUP BY td_cell_allocation.continent
                        ORDER BY tot_click DESC

它给了我这个:

shuvra         pan         2     europe  
shuvra         pan         2     europe  
saswat         roy         3     america  

然后我将查询更改为

SELECT *
FROM (

SELECT first_name, count( click_id ) AS tot_click, td_cell_allocation.continent
FROM td_cell_allocation, td_cell_click
WHERE td_cell_allocation.user_email = td_cell_click.user_mail
ORDER BY tot_click, timestamp DESC
) AS inv
GROUP BY inv.continent

它回来了

saswat  7   america

我希望我的结果是这样的:

    saswat         roy         5     america  
    shuvra         pan         4     europe  
4

2 回答 2

1

您看到奇怪结果的原因是您没有将 group by 中的所有非聚合列命名。Mysql(仅)有一些关于分组的非标准规则。

第一步是通过以下方式将其他列添加到组中:

SELECT first_name, last_name, count( click_id ) AS tot_click, td_cell_allocation.continent
FROM td_cell_allocation, td_cell_click
WHERE td_cell_allocation.user_email = td_cell_click.user_mail
AND date_of_click LIKE '%-06-%'
GROUP BY first_name, last_name, td_cell_allocation.continent
ORDER BY tot_click DESC

下一个改进是将您的查询更改为正确的连接:

SELECT first_name, last_name, count( click_id ) AS tot_click, td_cell_allocation.continent
FROM td_cell_allocation
JOIN td_cell_click ON td_cell_allocation.user_email = td_cell_click.user_mail
WHERE date_of_click LIKE '%-06-%'
GROUP BY first_name, last_name, td_cell_allocation.continent
ORDER BY tot_click DESC
于 2013-06-06T11:12:23.173 回答
0

试试这个

select t1.first_name,t1.last_name,count(t2.date_of_click) as cnt ,t1.continent 
from Table1 t1
Inner join Table2 t2
ON t1.user_email=t2.user_mail AND date_of_click LIKE '%-06-%'
Group BY continent
ORDER BY cnt DESC

SQL 小提琴演示

于 2013-06-06T11:04:15.113 回答