本文介绍了查找给定表中的第一个和最后一个记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
heyy ...... 我想查询,查找给定表格中的第一个和最后一个记录但我想要某些条件的结果.. 这里是我的表数据:
heyy... I want a query, to find the first and last records in a given table but I want the result by some condition.. here is my table data :
No. starttime Endtime ------- --------- -------- 0500000001 06:52 06:59 0500000001 07:00 07:27 0500000001 07:28 08:09 0500000001 08:14 08:40 0500000001 08:42 10:00 0500000001 10:01 11:29 0500000001 11:31 12:41 0500000001 12:43 13:28 0500000144 10:59 11:03 0500000144 11:03 11:04 0500000144 11:05 11:07 0500000144 11:08 11:37 0500000144 11:37 09:47 0500000144 09:47 09:51 0500000144 09:52 09:53 0500000144 09:54 10:46 0500000144 10:47 11:12 0500000144 11:13 11:16 0500000144 11:17 13:02 0500000144 13:02 09:29 0500000144 13:29 09:29这里我希望我的结果像这样
here I want My result as like this
No. starttime Endtime ------ ---------- -------- 0500000001 06:52 13:28 0500000144 10:59 09:29ie。第一条记录和最后一条记录号 任何想法如何在mysql中编写查询...?
ie. first record and last record of No. Any idea how to write query in mysql...?
推荐答案你 Google [ ^ ]它? 尝试这种格式.. 获得第一条记录: Do you Google[^] it? Try in this format.. To get the first record: select col1,col2,col3 from tab1 order by col1 asc limit 1;
获取最后一条记录:
To get the last record:
select col1,col2,col3 from tab1 order by col1 desc limit 1;
试试这样 Try like this DECLARE @Vehicle TABLE ( VehicleNo NVARCHAR(50),Datetime1 Datetime,Datetime2 Datetime); INSERT INTO @Vehicle (VehicleNo,Datetime1,Datetime2) VALUES('0500000001','06:52','06:59') INSERT INTO @Vehicle (VehicleNo,Datetime1,Datetime2) VALUES('0500000001','07:00','07:27') INSERT INTO @Vehicle (VehicleNo,Datetime1,Datetime2) VALUES('0500000144','10:59','11:03') SELECT top 1 * FROM @Vehicle WHERE VehicleNo=(SELECT MIN(VehicleNo) FROM @Vehicle) UNION SELECT top 1 * FROM @Vehicle WHERE VehicleNo=(SELECT MAX(VehicleNo) FROM @Vehicle)
stackoverflow/questions/2735395/how-to-find-first -and-last-record-from-mysql-table get-the-last-last-last-record-of-db [ ^ ] 检查链接..希望它有帮助.. stackoverflow/questions/2735395/how-to-find-first-and-last-record-from-mysql-table get-the-first-and-last-record-of-db[^] Check the link..hope it will help..
更多推荐
查找给定表中的第一个和最后一个记录
发布评论