ORDER BY 子句失败

ORDER BY clause failed

我的 ORDER BY 子句失败了。代码:

try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/kabumbu?autoReconnect=true&useSSL=false", "root", "****");
            String q = "select player_id, sum(number_of_goals) as 'Number of Goals',school_name "
                    + "from goal_scorers g , schools s "
                    + "where g.school_id = s.school_id "
                    + "group by g.player_id "
                    + "ORDER BY number_of_goals DESC";
            PreparedStatement pstm = conn.prepareStatement(q);
            ResultSet rs = null;
            rs = pstm.executeQuery();

            jTable2.setModel(DbUtils.resultSetToTableModel(rs));

    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, e);
    }

您查询的是 sum(number_of_goals) 所以 order by 应该使用相同的:

ORDER BY sum(number_of_goals)

而不是ORDER BY number_of_goals

或按列数 (2)

ORDER BY 2

ORDER BY { column-Name | ColumnPosition | Expression }

column-Name

Refers to the names visible from the SelectItems in the underlying query of the SELECT statement. The column-Name that you specify in the ORDER BY clause does not need to be the SELECT list.

ColumnPosition

An integer that identifies the number of the column in the SelectItems in the underlying query of the SELECT statement. ColumnPosition must be greater than 0 and not greater than the number of columns in the result table. In other words, if you want to order by a column, that column must be specified in the SELECT list.

Expression A sort key expression, such as numeric, string, and datetime expressions. Expression can also be a row value expression such as a scalar subquery or case expression.