MySQL:获取满足某些条件的最新记录

时间:2018-03-22 19:02:23

标签: mysql sql

我受到post的启发。但我要解决的问题更为复杂。

在下表中,我们有三列,idratingcreated,称之为test_table

+----+--------+----------------------+
| id | rating | created              |
+----+--------+----------------------+
|  1 | NULL   | 2011-12-14 09:25:21  |
|  1 | 2      | 2011-12-14 09:26:21  |
|  1 | 1      | 2011-12-14 09:27:21  |
|  2 | NULL   | 2011-12-14 09:25:21  |
|  2 | 2      | 2011-12-14 09:26:21  |
|  2 | 3      | 2011-12-14 09:27:21  |
|  2 | NULL   | 2011-12-14 09:28:21  |
|  3 | NULL   | 2011-12-14 09:25:21  |
|  3 | NULL   | 2011-12-14 09:26:21  |
|  3 | NULL   | 2011-12-14 09:27:21  |
|  3 | NULL   | 2011-12-14 09:28:21  |
+----+--------+----------------------+

我想编写一个查询,为每个rating选择最新的null但不选择id。如果特定null的所有评分均为id,我们会选择最新的rating。期望的结果如下:

+----+--------+----------------------+
| id | rating | created              |
+----+--------+----------------------+
|  1 | 1      | 2011-12-14 09:27:21  |
|  2 | 3      | 2011-12-14 09:27:21  |
|  3 | NULL   | 2011-12-14 09:28:21  |
+----+--------+----------------------+

5 个答案:

答案 0 :(得分:1)

以下内容获取创建日期:

select t.id,
       coalesce(max(case when rating is not null then creation_date end),
                creation_date
               ) as creation_date
from t
group by t.id;

然后你可以这样做:

select t.*
from t
where (id, creation_date) in (select t.id,
                                     coalesce(max(case when rating is not null then creation_date end),
                                     creation_date
                                    ) as creation_date
                              from t
                              group by t.id
                             );

答案 1 :(得分:0)

select a.* from #test a join (select id, max(created) created
from #test
where rating is not null
group by id )b on a.id=b.id and a.created=b.created
union 
select a.* from #test a join 

(select id, max(created) created
from #test
where rating is  null
and id not in 

(select id from  (select id, max(created) created
from #test
where rating is not null
group by id )d

group by id)
group by id )b on a.id=b.id and a.created=b.created

答案 2 :(得分:0)

一个可能的答案就是这个。创建每个id的最大(创建)日期列表以及具有所有NULL评级的id。

select t1.*
from myTable t1
join (
  select id, max(created) as created
  from myTable
  where rating is not NULL
  group by id 
   UNION ALL
  select id, max(created) as created
  from myTable t3
  where rating is NULL
  group by id 
  having count(*) = (select count(*) from myTable t4 where t4.id=t3.id)     
 ) t2
where t1.id=t2.id
and t1.created=t2.created
order by t1.id;

答案 3 :(得分:0)

此查询应该有效:

select a.id, a.rating, b.m from test_table a
join (
    select id, max(created) as m from test_table 
    where rating is not null 
    group by id 
) b on b.id = a.id and b.m = a.created
union 
select a.id, a.rating, b.m from test_table a
join( 
    select id, max(created) as m from test_table a
    where not exists 
        (select 1 from test_table b where a.id = b.id and b.rating is not null)
    group by id 
)b on b.id = a.id and b.m = a.created

答案 4 :(得分:0)

您可以在相关的created子查询中获取LIMIT 1值:

select t.id, (
  select created
  from mytable t1
  where t1.id = t.id
  order by rating is null asc, created desc
  limit 1
) as created
from (select distinct id from mytable) t

如果您还需要rating列,则需要再次将结果与表格相关联:

select t.*
from (
  select t.id, (
    select created
    from mytable t1
    where t1.id = t.id
    order by rating is null asc, created desc
    limit 1
  ) as created
  from (select distinct id from mytable) t
) x
natural join mytable t

演示:http://sqlfiddle.com/#!9/49e68c/8