春を学んでいます。
このクエリは MySQL で問題なく実行できます (もちろん)。
SELECT notes, month(dt) month, dayofmonth(dt) day, hour(dt) hour, minute(dt) minute,
avg(temperature2) temperature2, avg(temperature) temperature, avg(temperature1) temperature1
FROM temperature WHERE dt > curdate() - 1
GROUP BY notes, month(dt), dayofmonth(dt), hour(dt), minute(dt)
ORDER BY dt DESC
Pythonはこれをうまく行うことができます。
普通の古い Jdbc を使用するだけで動作します:
Connection conn = null ;
try {
System.out.println("Start try");
String driver = "com.mysql.jdbc.Driver";
Class.forName(driver).newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/brew", "root", "");
String sql = "SELECT notes, month(dt) month, dayofmonth(dt) day, hour(dt) hour, "
+ "minute(dt) minute, avg(temperature2) temperature2, avg(temperature) temperature, "
+ "avg(temperature1) temperature1, "
+ "FROM temperature WHERE dt > curdate() - 1 "
+ "GROUP BY notes, month(dt), dayofmonth(dt), hour(dt), minute(dt) "
+ "ORDER BY dt DESC ";
PreparedStatement ps = conn.prepareStatement(sql) ;
ResultSet rs = ps.executeQuery();
if (rs.next()) {
System.out.println(rs.getString("notes") + " " + rs.getDouble("temperature"));
} else {
System.out.println("No results");
}
rs.close();
ps.close();
しかし、Spring の JdbcTemplate ではありませんか?
public List<Temperature> getMinutelyTemperatures() {
String sql = "SELECT notes, month(dt) month, dayofmonth(dt) day, hour(dt) hour, "
+ "minute(dt) minute, avg(temperature2) temperature2, avg(temperature) temperature, "
+ "avg(temperature1) temperature1, "
+ "FROM temperature WHERE dt > curdate() - 1 "
+ "GROUP BY notes, month(dt), dayofmonth(dt), hour(dt), minute(dt) "
+ "ORDER BY dt DESC ";
//return namedParameterJdbcTemplate.query(sql, new MapSqlParameterSource(), new TemperatureMapper());
return jdbcTemplate.query(sql, new TemperatureMapper());
}
private static final class TemperatureMapper implements RowMapper<Temperature> {
@Override
public Temperature mapRow(ResultSet resultSet, int rowNum) throws SQLException {
Temperature temperature = new Temperature();
temperature.setNotes(resultSet.getString("notes"));
temperature.setDt(resultSet.getDate("dt"));
temperature.setTemperature(resultSet.getDouble("temperature"));
temperature.setTemperature1(resultSet.getDouble("temperature1"));
temperature.setTemperature2(resultSet.getDouble("temperature2"));
return temperature;
}
}
そして、サブクエリの列を使用して注文することさえできませんか?
public List<Temperature> getMinutelyTemperatures() {
String sql = "SELECT notes, month(dt) month, dayofmonth(dt) day, hour(dt) hour, "
+ "minute(dt) minute, avg(temperature2) temperature2, avg(temperature) temperature, "
+ "avg(temperature1) temperature1 "
+ "FROM (SELECT notes, dt, temperature2, temperature, temperature1 FROM temperature WHERE dt > curdate() - 1) b "
+ "GROUP BY notes, month(dt), dayofmonth(dt), hour(dt), minute(dt) "
+ "ORDER BY b.dt DESC ";
//return namedParameterJdbcTemplate.query(sql, new MapSqlParameterSource(), new TemperatureMapper());
return jdbcTemplate.query(sql, new TemperatureMapper());
}
私はここで何を見落としていますか?
エラーは次のとおりです。
nested exception is java.sql.SQLException: Column 'dt' not found.
order by 句から削除するか、where 句から削除すると、同じエラーが発生します。