0
String sql= "select amount,\n"+ "Temperature,\n"+ "Density\n"+
    "from sheet\n"+ "where Code=? and product=? and and month(date)=? && year(date)=? ";

PreparedStatement stmt=DBConnectionDATABASE.prepareStatement(sql);

try {
    stmt.setString(1, get.Code());
    stmt.setString(2, get.Product());
    stmt.setInt(3, get.month());
    stmt.setInt(4, get.Year());

data で前月の最終日を見つけるにはどうすればよいですか。

4

5 に答える 5

1

String sql= "select amount,\n"+ "Temperature,\n"+ "Density\n"+ "from sheet\n"+ "where Code=? and product=? and day(date)=? and month(date)=? && year(date) =? " and set parameters

于 2012-12-17T10:12:14.627 に答える
1
  1. 日付を less と比較します。where ... date<?
  2. パラメータを翌月の初めに設定します。これは、取得した月と年から計算できます。Calendar を参照してください
  3. 日付の降順でクエリを並べ替えます。where ... order by date desc
  4. クエリを制限するよりも最後のエントリのみが本当に必要な場合:where ... order by ... limit 1

クエリ全体は次のようになります。

select amount, Temperature, Density from sheet where Code=? and product=? and date < ? order by date desc limit 1
于 2012-12-17T09:05:12.177 に答える
0

このクエリを使用します

select max(date) from sheet where date between ? and ? 

これは、日付範囲間の最大の日付を返します

それから

select * from sheet where date=(select max(date) from sheet where date between ? and ?);

集計関数を使用しないで使用するのは倫理的ではないことは十分承知していますgroup byが、この場合は機能します。

于 2012-12-17T09:23:48.817 に答える
0

前月の最終日を取得するには

    SELECT DATE_SUB( CURDATE( ) , INTERVAL DAYOFMONTH( CURDATE( ) ) 
DAY ) AS  'last_date'
FROM dual

次に、クエリ文字列の日付パラメーターとして last_date

文字列 sql= "select amount,\n"+ "Temperature,\n"+ "Density\n"+ "from sheet\n"+ "where Code=? and product=? and date=? ";

于 2012-12-17T09:12:55.573 に答える
0

int 年 = 2012; int 月 = 1; int 日 = 0; cal.set(年、月、日);
日付 date = cal.getTime();

この日付をクエリに設定すると、2012 年 1 月 31 日が返されます

month =2 を指定すると、2012 年 2 月 29 日などになります。

于 2012-12-17T09:13:54.967 に答える