您好,我想知道的是在web开发中查询当前一个星期的信息或当前一个月信息是如何实现的。就是查询this week和this month的信息,具体的说就是如何用查询语句实现它

解决方案 »

  1.   

    java语言的话可以借助Calendar来实现。
    根据数据库的不同,也可以使用数据库自带的函数来实现。例如mysql:  select *  from   table where   month(时间字段名)=month(now())
      

  2.   

    SQLServer查询最近一天,三天,一周,一月,一季度方法 
    一天 "select * from T_news where datediff(day,addtime,getdate())=0";三天 "select * from T_news where datediff(day,addtime,getdate())<= 2 and datediff(day,addtime,getdate())>= 0";
    一周 "SELECT * FROM T_news WHERE (DATEPART(wk, addtime) = DATEPART(wk, GETDATE())) AND (DATEPART(yy, addtime) = DATEPART(yy, GETDATE()))";
    一月"SELECT * FROM T_news WHERE (DATEPART(yy, addtime) = DATEPART(yy, GETDATE())) AND (DATEPART(mm, addtime) = DATEPART(mm, GETDATE()))";
    一季度 "select * from t_news where DATEPART(qq, addtime) = DATEPART(qq, GETDATE()) and DATEPART(yy, addtime) = DATEPART(yy, GETDATE())";其中T_news是表名,addtime是数据库中对于的时间列
      

  3.   

    如果数据表中所要查询的那个日期建有索引的话,那在字段一边就不应该使用数据库函数。import java.sql.Connection;
    import java.sql.Date;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.Calendar;public class Test2 {    public static void main(String[] args) {
            
            // 当月
            Calendar c = Calendar.getInstance();
            clearTime(c);
            c.set(Calendar.DAY_OF_MONTH, 1);
            Date currentMonthStart = new Date(c.getTimeInMillis());
            c.add(Calendar.MONTH, 1);
            Date currentMonthEnd = new Date(c.getTimeInMillis());
                    
            // 当周
            Calendar c = Calendar.getInstance();
            clearTime(c);
            c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
            Date currentWeekStart = new Date(c.getTimeInMillis());
            c.add(Calendar.DAY_OF_MONTH, 7);
            Date currentWeekEnd = new Date(c.getTimeInMillis());
            
            Connection con = null;
            PreparedStatement ps = null;
            ResultSet rs = null;
            try {
                con = null; //
                String sql = "SELECT id, name, price FROM sales WHERE sales_date >= ? AND sales_date < ?";
                ps = con.prepareStatement(sql);
                ps.setDate(1, currentWeekStart);
                ps.setDate(2, currentWeekEnd);
                ...
            } catch (SQLException e) {
                ...
            } finally {
                ...
            }
        }
        
        private static void clearTime(Calendar c) {
            c.set(Calendar.HOUR_OF_DAY, 0);
            c.set(Calendar.MINUTE, 0);
            c.set(Calendar.SECOND, 0);
            c.set(Calendar.MILLISECOND, 0);
        }
    }
      

  4.   

    where month(myPostDatetime) = 12就行了,就能拿到12月份的内容了,这个是SQl语句的功能,不同的数据库可能有轻微不同。
      

  5.   

    用數據庫語句里面的between A and B查詢