我有一个查询,它给我提供了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'下面是结果
我正在使用sql-server-2005
我试过用TOP1,但这并没有给我最新的记录
发布于 2016-05-20 06:53:17
您需要在查询中添加一个ORDER BY。如果没有ORDER BY子句,就无法保证TOP命令将返回预期结果:
SELECT TOP 1
<column_list>
FROM ....
ORDER BY a.doc_date DESC此外,您还应该使用 syntax.
发布于 2016-05-20 06:55:05
请尝试以下查询:
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 DESChttps://stackoverflow.com/questions/37339714
复制相似问题