下策——查询出结果后将时间排序后取第一条 select * from a where create_time<="2017-03-29 19:30:36"order by create_time desclimit 1这样做虽然可以取出当前时间最近的一条记录,但是一次查询需要将表遍历一遍,对于百万以上数据查询将比较费时:limit是先取出全部结果,然后取第一条,相当于查询中占用了不必要的时间和空间:还有如果需要批量取出最近一条记录,比方说:“一个订单表,有用户,订单时间,金额,需…
sql 查询某个条件下多条数据中最新的一条数据或最老的一条数据 test_user表结构如下: 需求:查询李四.王五.李二创建的最初时间或者最新时间 1:查询最初的创建时间: SELECT * FROM( SELECT * FROM test_user ) AS tu WHERE NOT EXISTS ( SELECT * FROM( SELECT * FROM test_user ) AS tu2 WHERE tu2.user_name=tu.user_name AND DATE(tu.tim…
SELECT * FROM rsl a, (SELECT CODE, max(time_key) time_key FROM rsl GROUP BY CODE ) b WHERE a. CODE = b. CODE AND a.time_key = b.time_key AND a. CODE IN ('HK.00700', 'HK.03888'); table :rsl 然后查询出根据每一种的code 中最新的一组数据…
using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp3 { class Program { static LinkedList<int> sortArrayLink = new LinkedList<int>();…
1.取时间最新的记录 不分组有重复(多条CreateTime一样的都是最新记录) select * from test t where pid in ( select PId from Test t where time=(select max(time) from Test t1 where t1.PId=t.PId) group by Pid ) and time=(select max(time) from Test t1 where t1.PId=t.PId) 2.分组后取时间最新的记录…