MySQL - 将同一表中的行与日期范围进行比较

时间:2017-05-23 23:54:05

标签: mysql

我的MySQL查询有点问题。我需要比较同一个表中的行。这是我的表" video_stats":

----------------------------------
video       | date       | views
----------------------------------
8kCge8vWnIg | 2017-05-15 | 4
8kCge8vWnIg | 2017-05-16 | 20
8kCge8vWnIg | 2017-05-17 | 11
8kCge8vWnIg | 2017-05-18 | 12
rYyiTP0srNs | 2017-05-15 | 627
rYyiTP0srNs | 2017-05-16 | 6414
rYyiTP0srNs | 2017-05-17 | 21076
rYyiTP0srNs | 2017-05-18 | 34434
cRfEo-ZzU4U | 2017-05-17 | 17049
cRfEo-ZzU4U | 2017-05-18 | 12466

MySQL查询:

SELECT t1.video, 
       t1.date, 
       t1.views,
       t1c.video,
       t1c.date,
       t1c.views
FROM video_stats t1
LEFT JOIN video_stats t1c ON t1.video=t1c.video
           AND t1c.date<='2017-05-16' AND t1c.date>='2017-05-15'
WHERE t1.date>='2017-05-17'
      AND t1.date<='2017-05-18'

结果就在这里......

     --------------------------------------------------------------------------
     video       | t1.date    | t1.views | video       | t1c.date   | t1c.date
     --------------------------------------------------------------------------
 #1| 8kCge8vWnIg | 2017-05-17 | 12       | 8kCge8vWnIg | 2017-05-15 | 4
 #2| 8kCge8vWnIg | 2017-05-17 | 12       | 8kCge8vWnIg | 2017-05-16 | 20   
 #3| 8kCge8vWnIg | 2017-05-18 | 11       | 8kCge8vWnIg | 2017-05-15 | 4
 #4| 8kCge8vWnIg | 2017-05-18 | 11       | 8kCge8vWnIg | 2017-05-16 | 20
 #5| rYyiTP0srNs | 2017-05-17 | 21076    | rYyiTP0srNs | 2017-05-15 | 627
 #6| rYyiTP0srNs | 2017-05-17 | 21076    | rYyiTP0srNs | 2017-05-16 | 6414
 #7| rYyiTP0srNs | 2017-05-18 | 34434    | rYyiTP0srNs | 2017-05-15 | 627
 #8| rYyiTP0srNs | 2017-05-18 | 34434    | rYyiTP0srNs | 2017-05-16 | 6414
 #9| cRfEo-ZzU4U | 2017-05-17 | 17049    | NULL        | NULL       | NULL
#10| cRfEo-ZzU4U | 2017-05-18 | 12466    | NULL        | NULL       | NULL

t1.date和t1c.date中的单个日期没有问题。但是如果有多个日期问题就会开始。如何排除重复的行:#2和#3,#6和#7? 如何恰当地总结t1.views和t1c.views?

最后我需要这样的东西......但我不确切知道如何。

video        | t1.views *05/17 + 05/18* | t1c.views *05/15 + 05/16*
-------------------------------------------------------------------
8kCge8vWnIg  | 23                       | 24
rYyiTP0srNs  | 55510                    | 7041
cRfEo-ZzU4U  | 29515                    | NULL

2 个答案:

答案 0 :(得分:0)

Icky,但它确实有效:

select 
  r1.video,
  r1.views,
  r2.views
from
    (SELECT 
       t1.video, 
       sum(t1.views) as views
    FROM 
       test t1
    WHERE
       t1.date>='2017-05-17'
       AND t1.date<='2017-05-18'
       group by t1.video) r1
   left outer join
    (SELECT 
       t2.video, 
       sum(t2.views) as views
    FROM 
       test t2
    WHERE
       t2.date>='2017-05-15'
       AND t2.date<='2017-05-16' 
       group by t2.video) r2
    on r1.video = r2.video;  

答案 1 :(得分:0)

select a.Video,a.views as 'view *05/15 + 05/16*',b.views as 'iew *05/17 + 05/18*' from
(select Video,sum(views) as views from
(SELECT * FROM YOURTABLE) as a where date(`date`)>='2017-05-17' and date(`date`)<='2017-05-18'
GROUP BY Video) as a

left join

(select Video,sum(views) as views from
(SELECT * FROM YOURTABLE) as a where date(`date`)>='2017-05-15' and date(`date`)<='2017-05-16'
GROUP BY Video) as b
on a.Video = b.Video

测试结果:

8kCge8vWnIg 23      24
cRfEo-ZzU4U 29515   
rYyiTP0srNs 55510   7041