0

我正在学习春天。

我可以在 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 子句中删除它,我会收到相同的错误。

4

1 回答 1

1

您的选择声明

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

实际上并没有选择一列dt,所以当你要求它时你会得到一个例外

temperature.setDt(resultSet.getDate("dt"));

您的其他代码片段从不要求以dt.

如果你想要dt你需要的专栏SELECT

SELECT notes, dt, avg(temperature) temperature, ...
于 2013-09-11T03:19:50.087 回答