我有一个查询,它在sql
中给了我5条记录,但我想显示一条最新的记录。
以下是我的查询
Select a.mkey, c.type_desc DOC_TYPE, a.doc_no INWARD_NO,
convert(varchar, a.doc_date,103)date,
a.to_user, a.No_of_pages, Ref_No, d.type_desc DEPT_RECEIVED, b.first_name + ' ' +
b.last_name EMP_RECEIVED, b.first_name + ' ' + b.last_name NAME,
b.email
from inward_doc_tracking_hdr a , user_mst b ,type_mst_a c,
type_mst_a d
where a.to_user = b.mkey and a.doc_type = c.master_mkey
and a.dept_received = d.Master_mkey and a.to_user = '1260'
以下是结果
[![查询] [1] [1]
我正在使用sql-server-2005
我试过TOP1
,但它没有给我最新的记录
答案 0 :(得分:2)
您需要在查询中添加ORDER BY
。如果没有ORDER BY
子句,则无法保证TOP
命令将返回预期结果:
SELECT TOP 1
<column_list>
FROM ....
ORDER BY a.doc_date DESC
答案 1 :(得分:0)
尝试此查询:
SELECT TOP 1 a.mkey, c.type_desc DOC_TYPE, a.doc_no INWARD_NO,
convert(varchar, a.doc_date,103)date,
a.to_user, a.No_of_pages, Ref_No, d.type_desc DEPT_RECEIVED, b.first_name + ' ' +
b.last_name EMP_RECEIVED, b.first_name + ' ' + b.last_name NAME,
b.email
from inward_doc_tracking_hdr a , user_mst b ,type_mst_a c,
type_mst_a d
where a.to_user = b.mkey and a.doc_type = c.master_mkey
and a.dept_received = d.Master_mkey and a.to_user = '1260'
ORDER BY a.doc_date DESC