2015-11-01 70 views
1

我有這個MySQL選擇查詢:MySQL的 - 篩選記錄其日期是最大的

SELECT s.* 
FROM campaign_statistic AS s INNER JOIN campaign AS c ON s.campaign_id = c.campaign_id 
WHERE c.campaign_id = 51 
AND date(s.created_date) BETWEEN date(c.start_date) AND date(c.end_date) 

並得到這樣的結果:

id campaign_id  instagram_id media_id      has_tag  comments_count likes_count  created_date   status 
** ***********  ************ **************************** ******** ************* ***********  *******************  ****** 
1 51    1230544324  957801995790641919_1230544324 together 1    16    2015-11-01 13:10:29  1 
2 51    1230544324  957799073015065299_1230544324 together 2    11    2015-11-01 13:10:29  1 
3 51    1230544324  957790310736521811_1230544324 together 5    20    2015-11-01 13:10:29  1 
4 51    1230544324  957801995790641919_1230544324 together 1    16    2015-11-02 13:10:29  1 
5 51    1230544324  957799073015065299_1230544324 together 2    11    2015-11-02 13:10:29  1 
6 51    1230544324  957790310736521811_1230544324 together 5    20    2015-11-02 13:10:29  1 
7 51    1230544324  957801995790641919_1230544324 together 1    16    2015-11-03 13:10:29  1 
8 51    1230544324  957799073015065299_1230544324 together 2    11    2015-11-03 13:10:29  1 
9 51    1230544324  957790310736521811_1230544324 together 5    20    2015-11-03 13:10:29  1 

但我試圖讓只有那些記錄其created_date是最大從以上結果和最大created_date2015-11-03

我需要這樣的結果:

id campaign_id  instagram_id media_id      has_tag  comments_count likes_count  created_date   status 
** ***********  ************ **************************** ******** ************* ***********  *******************  ****** 
7 51    1230544324  957801995790641919_1230544324 together 1    16    2015-11-03 13:10:29  1 
8 51    1230544324  957799073015065299_1230544324 together 2    11    2015-11-03 13:10:29  1 
9 51    1230544324  957790310736521811_1230544324 together 5    20    2015-11-03 13:10:29  1 

任何想法或建議?

謝謝。

回答

0

下面是使用not exists,讓每campagin_id

SELECT s.* 
FROM campaign_statistic AS s INNER JOIN campaign AS c ON s.campaign_id = c.campaign_id 
WHERE c.campaign_id = 51 
AND date(s.created_date) BETWEEN date(c.start_date) AND date(c.end_date) 
AND NOT EXISTS (
    SELECT 1 FROM campaign_statistic AS s2 
    INNER JOIN campaign c2 ON s2.campaign_id = c2.campaign_id 
    WHERE s2.campaign_id = s.campaign_id 
    AND s2.created_date > s.created_date 
) 
+0

嗨。我試過你的查詢但得到0結果。 –

+1

也許會拋出一個模糊的骨頭給他一個sqlfiddle – Drew

+0

@PHPBaby,在子查詢中缺少內部連接,請再試一次 – FuzzyTree

0

有辦法使用子查詢做到這一點具有最大created_date行的方式,但我認爲最簡單的就是使用由於加入和變量過濾:

SELECT cs.* 
FROM (SELECT s.* , 
      (@rn := if(@d = date(s.created_date), @rn, 
         if(@d := date(s.created_date), @rn + 1, @rn + 1) 
         ) 
      ) rn 
     FROM campaign_statistic s INNER JOIN 
      campaign c 
      ON s.campaign_id = c.campaign_id CROSS JOIN 
      (SELECT @d := 0, @rn := 0) params 
     WHERE c.campaign_id = 51 AND 
      date(s.created_date) BETWEEN date(c.start_date) AND date(c.end_date) 
     ORDER BY s.created_date desc 
    ) cs 
WHERE rn = 1; 

的變量都在做,基本上,在created_datedense_rank()

我建議寫WHERE子句:

 WHERE s.campaign_id = 51 AND 
      s.created_date >= DATE(c.start_date) AND 
      s.created_date < DATE_ADD(DATE(c.end_date), interval 1 day) 

那麼我想查詢可以campaign_statistic(campaign_id, created_date)採取指數的優勢。

1

你能嘗試運行此:

SELECT s.* 
FROM campaign_statistic AS s INNER JOIN campaign AS c ON s.campaign_id = c.campaign_id 
WHERE c.campaign_id = 51 
AND date(s.created_date) BETWEEN date(c.start_date) AND date(c.end_date) 
    AND date(s.created_date) = (
    SELECT date(max(s.created_date)) 
FROM campaign_statistic AS s INNER JOIN campaign AS c ON s.campaign_id = c.campaign_id 
WHERE c.campaign_id = 51 
AND date(s.created_date) BETWEEN date(c.start_date) AND date(c.end_date)) 
+0

@ php-當解釋計劃被創建時,寶貝會自動優化。 – sdayal

相關問題