我在 Spring Batch 项目中用于 JDBCPagingItemReader 的查询是:
SELECT
account_login_log_id log_id,
created_at reference_time
FROM account_login_log
WHERE account_login_log_id > 1000
ORDER BY account_login_log_id
RowMapper 捕获从读取器获取的项目并创建一个名为 SomeGeneralLog 的对象。
SomeGeneralLog log = new SomeGeneralLog();
log.setLogId(rs.getInt("log_id");
log.setReferenceTime(rs.getTimestamp("reference_time");
并且,将此日志返回给编写者,就像某些批处理进程所做的那样。
重点来了。程序抛出 SQLException! java.sql.SQLException:未找到列“account_login_log_id”。
什么?我什至没有在 RowMapper 代码中找到“account_login_log_id”列。 实际上,我可以通过将 ORDER BY 子句中的列名替换为“log_id”来使其运行。但为什么?原因是什么?请尽可能多地做笔记。 (我怀疑查询中关键字的查询处理顺序。*ref: https://stackoverflow.com/a/31808872/3648833 )
谢谢。
最佳答案
SELECT account_login_log_id log_id
您为该列指定了别名,因此现在它有一个新名称,并且您必须使用该新名称。
您的具体情况可能会因 RDBMS 的不同而有所不同。
关于mysql - SQL 别名可以出现在 ORDER BY 子句中,但不能出现在 WHERE 子句中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33388327/