编写查询以从数据库中检索记录 [英] write query to retrieve records from database

查看:73
本文介绍了编写查询以从数据库中检索记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须编写查询以从数据库中检索那些从今天起存在以前日期的记录..



就像今天是12/12/2012。此查询将检查上一个日期(12/11/2012)。如果它存在然后获取记录...如果它不存在然后它将检查(12/10/2012)等等...



希望你理解我的问题..

解决方案

尝试

  SELECT  *  FROM  YourTable 
WHERE DateCol =

SELECT MAX(DateCol) AS DateCol FROM YourTable
WHERE DATEDIFF(DAY,GETDATE(),DateCol)< 0





如果您有任何疑问,请告诉我。


试试这个查询:

 TimeSpan ts =  new  TimeSpan( 1  0  0  0 

select * from test_table where [DATE] < ' + System.DateTime.Now.ToShortDateString()+' 和[DATE]> ' + System.DateTime.Now.Subtract(ts).ToShortDateString()+'



现在你可以减去你想要的日期值。


  SELECT  * 来自 test_table 
WHERE [ DATE ]>(SYSDATE-30 )
AND [ DATE ]< SYSDATE


i have to write query to retrieve that records from database which have whatever previous date exists from todays date..

like today is 12/12/2012. and this query will check for previous date (12/11/2012). if it exists then fetch records... if it does not exist then it will check for (12/10/2012) and so on...

hope you understand my question..

解决方案

Try

SELECT * FROM YourTable
WHERE DateCol =
(
	SELECT MAX(DateCol) AS DateCol FROM YourTable
	WHERE DATEDIFF(DAY,GETDATE(),DateCol) < 0
)



Let me know if you have any questions.


try this query :

TimeSpan ts= new TimeSpan(1,0,0,0)
            
"select * from test_table where [DATE] < "'+System.DateTime.Now.ToShortDateString()+'" and [DATE] > "'+System.DateTime.Now.Subtract(ts).ToShortDateString()+'""


now you can subtract day values as much you want.


SELECT * from test_table
           WHERE   [DATE] > (SYSDATE-30)
           AND [DATE] < SYSDATE


这篇关于编写查询以从数据库中检索记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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