我正在尝试用准备好的声明获取表格中的最后一条记录。我在下面尝试了两个查询,但是当表中只包含一条记录时,我遇到了问题。
当我单独执行查询时,我得到结果但是当我在preparedStatement中执行它时,没有输入while
循环。即使表中包含一条带有预处理语句的记录,如何获取最后一条记录?
我感谢任何帮助。
第一次查询:
select stop_name
from behaviour
where mac = ?
order by behaviour_id desc
limit 1
第二次查询:
select stop_name
from behaviour
where created_at in (select max(created_at) from behaviour)
and mac = ?
表格
CREATE TABLE IF NOT EXISTS behaviour(
behaviour_id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
mac VARCHAR(30) NOT NULL,
stop_name VARCHAR(30) NOT NULL,
stop_distance INT(11) NOT NULL,
speed INT(11) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP)
代码
String sql = "select stop_name from behaviour where mac = ? order by behaviour_id desc limit 1";
//String sql = "select stop_name from behaviour where created_at in (select max(created_at) from behaviour) and mac = ?";
PreparedStatement preparedLastStopName = con.prepareStatement(sql);
preparedLastStopName.setString(1, macD);
ResultSet rsLastStopName = preparedLastStopName.executeQuery();
String sto_nam = "";
while (rsLastStopName.next()) {
sto_nam = rsLastStopName.getString("stop_name");
}
答案 0 :(得分:1)
通过insert_date desc
从mytable订单中选择前1 *