【问题标题】:Trying to get specific month and count value from MySQL into variables试图从 MySQL 中获取特定月份和计数值到变量中
【发布时间】:2021-10-12 06:18:56
【问题描述】:

我在 MySQL 中运行此脚本来检索按特定类型和月份分组的约会计数:

从约会中选择类型、月份(开始)、计数(类型) GROUP BY 类型,月份(开始);

这为我提供了这张桌子: [1]:https://i.stack.imgur.com/sRJwx.png

这太棒了!现在我的问题是尝试将数据放入变量中:

String sql = "SELECT `Type`, month(`Start`), COUNT(Type) FROM appointments " +
                "GROUP BY `Type`, month(`Start`)";

        try(Statement s = DatabaseConnection.getConnection().createStatement();
        ResultSet rs = s.executeQuery(sql)) {
            while(rs.next()) {
                String typeKey = rs.getString("Type");
                int monthValue = rs.getInt("month(Start)");
                int count = rs.getInt("COUNT(Type)");

当我运行它时,我得到一个 SQLException,说找不到列“月(类型)。我尝试使用列名“开始”并尝试仅使用“月”并收到相同的 SQLException(我知道,有点愚蠢的故障排除,但值得一试)。我环顾四周寻找一些解决方案,但我做不到。

这是一个学校项目,所以我只能使用 JDBC 和 Java 特定的驱动程序。如果有人能指出我正确的方向,我将不胜感激。

【问题讨论】:

    标签: java mysql jdbc


    【解决方案1】:

    尝试使用as重命名列

    sql = "SELECT `Type` as type ,
                   month(`Start`) as mstart,
                   COUNT(Type) as ctype
                   FROM appointments " +
                   "GROUP BY `Type`, month(`Start`)";
    

    【讨论】:

    猜你喜欢
    • 2021-05-23
    • 1970-01-01
    • 1970-01-01
    • 2012-04-15
    • 1970-01-01
    • 2021-12-02
    • 2019-05-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多