我有这样的数据库表数据:
cookie_id cookie_name cookie_url cookie_view date time
1 S265nEmXMz /test.php 1 23/11/16 02:51:39
2 S265nEmXMz /test.php 2 23/11/16 02:52:45
3 5h2Rae6YRO /test.php 1 23/11/16 02:52:58
我想让cookie_name具有最大视图数和减去最大到最小时间,如
cookie_id cookie_name cookie_url cookie_view date time
1 S265nEmXMz /test.php 2 23/11/16 00:01:06
2 5h2Rae6YRO /test.php 1 23/11/16 02:52:58
答案 0 :(得分:0)
您应该使用subtime来获取时差。另一个细节是,当你只有一个cookie以避免时间为0时,你需要CASE。
SELECT `cookie_id`,
`cookie_name`,
`cookie_url`,
MAX(`cookie_view`) cookie_views,
`date`,
( CASE
WHEN Count(`cookie_id`) > 1 THEN Subtime(Max(`time`), Min(`time`))
ELSE time
end ) time_in
FROM `cookies`
GROUP BY `cookie_name`
ORDER BY cookie_id
答案 1 :(得分:0)
尝试:
SELECT cookie_name,
cookie_url,
Max(cookie_view) AS maxview,
Subtime(Max(time), Min(time)) AS spent,
date
FROM `cookie_table`
GROUP BY cookie_name