mysql 排序语句的写法,网上真是林林总总,但能用的不多。甚至有很多写法,从语法上看,就根本没有排序的样子。
求人不如求己,自己来吧。
有两种写法:
1.根据需要调整正序还是倒序
select * from tabnamexxxx t
where t.id=(select id from tabnamexxxx t2 where t2.com = t.com order by t2.updatetime desc limit 1)
and t.com in ('xxxx','xxxxx')
GROUP BY t.com ;
2.更加的简单粗暴,如果是少量的数据,直接单条数据查询,然后 union all,适合工具一次性查询,肯定是不适合在项目中用的,会被领导打死的(此为免责声明)。
select * from
(select * from tablexxxxxx t where t.com = 'xxxxxx' order by t.updatetime desc limit 1) t
union all
select * from
(select * from tablexxxxxx t where t.com = 'xxxxxx' order by t.updatetime desc limit 1) t
union all
select * from
(select * from tablexxxxxx t where t.com = 'xxxxxx' order by t.updatetime desc limit 1) t
union all
select * from
(select * from tablexxxxxx t where t.com = 'xxxxxx' order by t.updatetime desc limit 1) t ;