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());
我怎样才能找到上个月的最后一天的数据。
答案 0 :(得分:1)
where ... date<?
where ... order by date desc
where ... order by ... limit 1
整个查询可能如下所示:
select amount, Temperature, Density from sheet where Code=? and product=? and date < ? order by date desc limit 1
答案 1 :(得分:1)
String sql =“select amount,\ n”+“Temperature,\ n”+“Density \ n”+“from sheet \ n”+“where Code =?and product =?and day(date)=?和月(日期)=?&amp;&amp; year(date)=?“并设置参数
答案 2 :(得分:0)
要获得上个月的最后一天
SELECT DATE_SUB( CURDATE( ) , INTERVAL DAYOFMONTH( CURDATE( ) )
DAY ) AS 'last_date'
FROM dual
然后将last_date作为查询字符串日期参数
String sql =“select amount,\ n”+ “温度,\ n” + “密度\ n” + “来自表格\ n”+ “Code =?and product =?and date =?”;
答案 3 :(得分:0)
int year = 2012;
int month = 1;
int day = 0;
cal.set(年,月,日);
日期日期= cal.getTime();
将此日期设置为您的查询 这将给jan 31 2012
如果您给出月份= 2,则会给予2012年2月29日等等
答案 4 :(得分: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
的情况下使用聚合函数是不合乎道德的,但在这种情况下它是有效的。