查找给定表中的第一个和最后一个记录 [英] To find the first and last records in a given table

查看:69
本文介绍了查找给定表中的第一个和最后一个记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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:29





ie。第一条记录和最后一条记录号



任何想法如何在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)


http://stackoverflow.com/questions/2735395/how-to-find-first -and-last-record-from-mysql-table



get-the-last-last-last-record-of-db [ ^ ]



检查链接..希望它有帮助..
http://stackoverflow.com/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..


这篇关于查找给定表中的第一个和最后一个记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆