数据库中的最新条目 [英] Latest Entry in the database

查看:58
本文介绍了数据库中的最新条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个桌子名字

病人

ptn_no ipno admission_date

1 1 12/12/11

1 2 14/12/11





i想要此表中的最新条目为ptn_no 1.



帮助我进行Sql查询

如何通过入场日期匹配最新条目

解决方案

如果您有主键或唯一标识列,只需按降序排序,您将获得最新记录。


要使用入场日期列获取最新记录,查询可以用几种不同的方式构建。



一种方法是使用非相关条件选择它,例如:

< pre lang =SQL> SELECT *
FROM 患者
WHERE Admission_Date =( SELECT M AX(Admission_Date) FROM 患者)



以上只有在入学日期没有重复的情况下才有效。



另一种选择可能是使用TOP,例如:

  SELECT   TOP  1 
ptn_no,
admission_date
FROM 患者
ORDER BY admission_date DESC



如果入学日期有重复,你基本上会得到最新录取日期的随机记录。


使用录取日期的desc顺序获取记录,在类似日期的情况下ipno将通过ipno给你最新的录入

  SELECT   top   1  * 
FROM 患者
其中 ptn_no = 1
ORDER BY admission_date Ipno DESC



快乐编码!

:)


I have A table name
Patient
ptn_no ipno admission_date
1 1 12/12/11
1 2 14/12/11


i want the latest entry in this table for ptn_no 1.

Help me for the Sql Query
how can i match the latest entry through admission date

解决方案

If you have a primary key or unique identity column, just sort by that descending and you will get the latest record right on top.


To get the latest record using the admission date column, the query can be built in several different ways.

One way is to select it using a non-correlated condition, for example:

SELECT *
FROM Patient
WHERE Admission_Date = (SELECT MAX(Admission_Date) FROM Patient)


The above will work only if there is no duplicates with the admission date.

Another option could be to use TOP, for example:

SELECT TOP(1)
       ptn_no,
       admission_date
FROM Patient
ORDER BY admission_date DESC


Again if there are duplicates for admission date, you will basically get a random record for the latest admission date.


get records using desc order of admission date and ipno in case of similar date it will give you latest entry by ipno

SELECT top 1 *
FROM Patient 
where ptn_no = 1 
ORDER BY admission_date, Ipno DESC


Happy Coding!
:)


这篇关于数据库中的最新条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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