我有这个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_date
是2015-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 :(得分: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))
答案 1 :(得分:0)
以下是使用not exists
获取每created_date
个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
)
答案 2 :(得分:0)
有一些方法可以使用子查询来执行此操作,但我认为最简单的是使用变量,因为连接和过滤:
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;
变量基本上是dense_rank()
列上的created_date
。
我建议将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)
上的索引。