Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL-使用多个表按天选择过去7天的最后一个值_Mysql_Sql_Date_Statistics_Inner Join - Fatal编程技术网

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

MySQL-使用多个表按天选择过去7天的最后一个值,mysql,sql,date,statistics,inner-join,Mysql,Sql,Date,Statistics,Inner Join,以下是我当前的数据库: TABLE profileData +---------------------------+ | profileID | name | +----+----------------------+ | 1 | Stackoverflow | | 2 | Stackexchange | | 3 | Askubuntu | +-----------+---------------+ TABLE sta

以下是我当前的数据库:

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)
。这是可行的,但结果不包含相应的
scont

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


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

您可以做类似的事情,这与您所做的并不遥远

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
请参见

关于这一点(解释更好),因为需要更少的完整表格扫描。。 但你真的需要索引

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
;

请参见演示

哦,哇。这似乎真的奏效了,只是证实了我的“过度杀戮”是正确的。Tyvm.GROUP BY不应该是
stats.profileID,DATE(ts)
?不过,谢谢你。这是正确的,请看这里,这是不对的。。profileData.name将是随机的,并可能导致错误的值。。这是您应该始终使用sql\u mode=ONLY\u FULL\u GROUP\u BY…配置MySQL服务器的方式。。。