MySQL - 使用多个表选择过去7天内的最后一天

时间:2013-10-11 21:51:37

标签: mysql sql date statistics inner-join

这是我目前的数据库:

TABLE profileData
+---------------------------+
| profileID | name          |
+----+----------------------+
| 1         | Stackoverflow |
| 2         | Stackexchange |
| 3         | Askubuntu     |
+-----------+---------------+

TABLE stats
+----+-----------+------------------------------+
| id | profileID | sCount | ts                  |
+----+-----------+--------+---------------------+
| 1  | 1         | 1      | 2013-10-04 00:00:01 |
| 2  | 2         | 5      | 2013-10-04 00:00:01 |
| 3  | 3         | 8      | 2013-10-04 00:00:01 |
| 4  | 1         | 10     | 2013-10-05 00:00:01 |
| 5  | 2         | 50     | 2013-10-05 00:00:01 |
| 6  | 1         | 100    | 2013-10-06 00:00:01 |
| 7  | 2         | 500    | 2013-10-06 00:00:01 |
| 8  | 1         | 101    | 2013-10-06 13:00:01 |
| 9  | 2         | 501    | 2013-10-06 19:00:01 |
| 10 | 3         | 17     | 2013-10-06 05:00:01 |
| 11 | 1         | 100    | 2013-10-09 00:00:01 |
| 12 | 2         | 500    | 2013-10-09 00:00:01 |
+----+-----------+------- +---------------------+

TABLE users
+--------+-----------+
| userID | profileID |
+--------------------+
| 1337   | 1         |
| 1337   | 2         |
+--------+-----------+

我需要的是以下内容:

从“用户”表中选择所有配置文件,并从过去7天内这些配置文件的“统计信息”表中获取它们的名称+每天的最后一个条目。所以,预期的结果是

+---------------+--------+---------------------+
| name          | sCount | ts                  |
+---------------+--------+---------------------+
| Stackoverflow | 1      | 2013-10-04 00:00:01 |
| Stackexchange | 5      | 2013-10-04 00:00:01 |
| Stackoverflow | 10     | 2013-10-05 00:00:01 |
| Stackexchange | 50     | 2013-10-05 00:00:01 |
| Stackoverflow | 101    | 2013-10-06 13:00:01 |
| Stackexchange | 501    | 2013-10-06 19:00:01 |
| Stackoverflow | 100    | 2013-10-09 00:00:01 |
| Stackexchange | 500    | 2013-10-09 00:00:01 |
+---------------+--------+---------------------+

我最后发表了这样的声明:

SELECT
    profileData.name,
    stats.scount,
    stats.ts
FROM
    users
INNER JOIN profiles ON 
    users.profileID = profiles.profileID
INNER JOIN
        (
SELECT t1.profileID, t1.sCount, t1.ts
FROM stats t1
INNER JOIN (
SELECT MAX(ts) maxi
FROM stats
GROUP BY DATE(ts)
        ) a2 ON t1.ts = a2.maxi) stats ON
            users.profileID = stats.profileID
WHERE
    users.userID = 1337 AND DATE(stats.ts) >= DATE(DATE_SUB(NOW(), INTERVAL 7 DAY))
ORDER BY users.userID, stats.ts

这部分起作用。然而,这句话似乎是一种矫枉过正,而且它已经无法使用了。

我也试过选择MAX(ts)。这样做有效,但结果不包含相应的sCount值。

所以,我正在寻找我的问题的解决方案,我希望,任何人都可以帮助我。

哦,如果可能的话,它必须是一个纯SQL解决方案。

2 个答案:

答案 0 :(得分:1)

你可以做那样的事情,这与你所做的并不相同。

select p.name,
       s.scount,
       s.ts
from profileData p
inner join users u on u.profileID = p.profileID
inner join stats s on s.profileID = p.profileID
inner join (select max(ts) as maxTs, profileID
            from stats
            where DATE(stats.ts) >= DATE(DATE_SUB(NOW(), INTERVAL 7 DAY))
            group by profileID, DATE(ts)) as mx
         on s.profileID = mx.profileID and mx.maxTs = s.ts
where u.userID = 1337

请参阅sqlfiddle

答案 1 :(得分:0)

这个怎么样(EXPLAIN更好),因为需要更少的FULL表扫描。 但你真的需要索引

SELECT 
   profileData.name
 , stats.sCount
 , MAX(ts) ts

FROM 
 stats

INNER JOIN
 users
ON
 stats.profileID = users.profileID

INNER JOIN
 profileData
ON
 users.profileID = profileData.profileID

WHERE
  users.userID = 1337
 AND
  DATE(stats.ts) >= DATE(DATE_SUB(NOW(), INTERVAL 7 DAY))

GROUP BY
   profileData.name
 , stats.profileID ASC
 , stats.sCount ASC

ORDER BY 
 ts ASC
;

参见演示http://sqlfiddle.com/#!2/db8d3/51