假设,我有一张桌子
pages_urls
+----+---------+---------------+
| id | site_id | download_date |
+----+---------+---------------+
| 1 | 1 | 2012-01-01 |
| 2 | 1 | 2012-12-31 |
| 3 | 2 | 2012-01-01 |
| 4 | 2 | 2012-12-31 |
+----+---------+---------------+
对于我要选择的每个 site_id:
- 最后下载日期
- 此站点的表中的记录数
我试过这个查询:
SELECT
pages_urls.site_id,
max_table.download_date,
COUNT(*)
FROM
pages_urls
LEFT JOIN
(
SELECT
site_id,
MAX(download_date) AS download_date
FROM
pages_urls AS max_pages_urls
WHERE
max_pages_urls.site_id=site_id
) AS max_table
ON
pages_urls.site_id=max_table.site_id
GROUP BY
site_id;
但我得到了这个而不是预期的结果:
desired result ║ my query result
+---------+---------------+----------║---------+---------------+----------+
| site_id | download_date | COUNT(*) ║ site_id | download_date | COUNT(*) |
+---------+---------------+----------║---------+---------------+----------+
| 1 | 2012-12-31 | 2 ║ 1 | 2012-12-31 | 2 |
| 2 | 2012-12-31 | 2 ║ 2 | NULL | 2 |
+---------+---------------+----------║---------+---------------+----------+
║
怎样才能得到必要的信息?