我有一个名为 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