This topic has been archived. It cannot be replied.
-
工作学习 / IT技术讨论 / 请问高手:在oracle中,如何用一个select语句选择最近日期的一条记录呢?
-lilyba(Sunshine);
2002-8-1
(#669253@0)
-
Do you mean 'Select Max( date) from table' ?
-cloud2001(卷云溶月);
2002-8-1
(#669259@0)
-
yes, hehe. thanks
-lilyba(Sunshine);
2002-8-1
(#669260@0)
-
You're welcome. :-)
-cloud2001(卷云溶月);
2002-8-1
(#669263@0)
-
but not enough. "select * from table where date=max(date)", can i write it in this way?
-lilyba(Sunshine);
2002-8-1
(#669261@0)
-
select * from table where date = (select max(date) from table)
are you saying just ONE record ?
-rootbear(啤酒);
2002-8-1
(#669265@0)
-
maybe not only one record. thanks, buddy.
-lilyba(Sunshine);
2002-8-1
(#669268@0)
-
when you say, buddy, to someone, you're supposed to拍着人家的肩膀,象MULAN一样,憋粗嗓子,而且还要个头够高,这个恐怕,,,呵呵
-rootbear(啤酒);
2002-8-1
{75}
(#669328@0)
-
no, I think you need subquery.
-cloud2001(卷云溶月);
2002-8-1
(#669267@0)
-
date time or only date?if "最近日期" means date only,
select * from tablename where to_char(datefield,'yyyy/mm/dd')=(select to_char(max(datefield),'yyyy/mm/dd') from tablename)
else if it means date plus time , use rootbear's answer. 即使这种情况下也有可能返回多条纪录,因为oracle的date字段精确到秒。只要一条纪录: rownum=1。
-bluebluesky(bluebluesky);
2002-8-2
{314}
(#670101@0)
-
where date=max(date) 这种写法到是让IT大小虾米跌眼镜。。很多。
-bluebluesky(bluebluesky);
2002-8-2
(#670111@0)
-
您这样大概会把整个table锁住
-uid(㊣Miracle);
2002-8-2
(#670117@0)
-
呵呵,你到是给锁个看看?
-bluebluesky(bluebluesky);
2002-8-2
(#670146@0)
-
e.g. select username, max(access_time) last_access_time, count(*) no_of_logins
from session_info_table
group by username
-uid(㊣Miracle);
2002-8-2
(#670113@0)
-
如果你要最“相近”的日期,也可能大于当前日期的化, I mean "closest date" 不是 "latest date" e.gselect *
from Customers
, Orders
where Customers.ID = Orders.Cust_ID
and Orders.OrderDate =
( select max(OrderDate)
from Orders
where Cust_ID = Customers.ID
and OrderDate < somedate
)
still Assume before someday
-uid(㊣Miracle);
2002-8-2
{285}
(#670124@0)