按特定条件排序

时间:2017-04-18 19:33:10

标签: mysql sql

我在MySQL中有一个表 -

 ABC (User, Login_time, City)

并包含一些数据。

我想要的结果是前5位用户表单Noida应该位于结果集的顶部,其余的结果集应该按login_time排序。请看这里的小提琴 -

http://www.sqlfiddle.com/#!9/88c203/9

create table abc (user varchar(20), login_time date, city varchar(20));

insert into abc values ('A', curdate(), 'Noida'),
                   ('B', '2017-04-16', 'Delhi'),
                   ('C', '2017-04-14', 'Noida'),
                   ('D', '2017-04-18', 'Kanpur'),
                   ('E', '2017-04-13', 'Raebareli'),
                   ('F', '2017-04-16', 'Sitapur'),
                   ('G', '2017-04-17', 'Muradabad'),
                   ('H', '2017-04-18', 'Noida'),
                   ('I', '2017-04-13', 'Lucknow'),
                   ('J', '2017-04-12', 'Noida');

预期结果是 -

'A', 2017-04-19, 'Noida'
'C', '2017-04-14', 'Noida'
'H', '2017-04-18', 'Noida'
'D', '2017-04-18', 'Kanpur'
'G', '2017-04-17', 'Muradabad'
'B', '2017-04-16', 'Delhi'
'F', '2017-04-16', 'Sitapur'
'E', '2017-04-13', 'Raebareli'
'I', '2017-04-13', 'Lucknow'
'J', '2017-04-12', 'Noida'

我试过的查询是 -

select * from (select user, login_time, city from abc
where city = 'Noida'
limit 3
union all 
select user, login_time, city from abc
where user not in (select user, login_time, city from abc
where city = 'Noida'
limit 3)
limit 4, count)
order by login_time

当我尝试在小提琴中执行查询时,它会抛出错误 -

This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery

非常感谢任何帮助。

4 个答案:

答案 0 :(得分:1)

您可以使用括号组合单独排序的结果。

(select user, login_time, city 
 from abc
 where city = 'Noida'
 --order by login_time desc > add this to sort by login_time 
 limit 5
)
union all
(select user, login_time, city 
 from abc
 where city <> 'Noida'
 order by login_time desc
 limit 3
)

因为您必须在结果集中包含Noida的非前5行,您可以使用left join来获取这些行并按login_time进行排序。

(select user, login_time, city 
 from abc
 where city = 'Noida'
 order by login_time desc
 limit 3
)
union all
(select a1.user, a1.login_time, a1.city 
 from abc a1
 left join (select user, login_time, city 
            from abc
            where city = 'Noida'
            order by login_time desc
            limit 3) a2 --this derived table is the previous result-set of top n users from Noida
 on a1.user=a2.user and a1.city= a2.city
 where a2.user is null       
 order by a1.login_time desc
 limit 7
 )

使用预准备语句获取前3行以外的剩余行数。

set @a= (select count(*) - 3 from abc);

prepare stmt from
'(select user, login_time, city 
  from abc
  where city = ''Noida''
  order by login_time desc
  limit 3)
 union all
(select a1.user, a1.login_time, a1.city 
 from abc a1
 left join (select user, login_time, city 
            from abc
            where city = ''Noida''
            order by login_time desc
            limit 3) a2
 on a1.user=a2.user and a1.city= a2.city
 where a2.user is null       
 order by a1.login_time desc
 limit ?
 )';

execute stmt using @a;

答案 1 :(得分:0)

如果您想首先使用诺伊达的用户,请使用:

select abc.*
from abc
order by (city = 'Noida') desc,
         login_time desc;

答案 2 :(得分:0)

如果您确定在city ='noida'中有3条记录>

select * 
from
(
    (
      select user,login_timecity
      from abc
      where city='noida'
      order by login_time
      limit 3
    )
    union
    (
      select user,login_timecity
      from abc
      where x
      order by city='noida' DESC, login_time
      limit 3, x
    )
) t

答案 3 :(得分:0)

尝试这样的事情:

select *, 1 as OrderHelp
    from abc
    where city = 'Noida'
    limit 3
union all
select *, 2 as OrderHelp
    from abc
    where user not in (
        select user
            from abc
            where city = 'Noida'
            limit 3
    )
    order by OrderHelp, login_time desc