我试图从mysql数据库的单列中只检索最近添加的最后3行。请帮个忙--
我的jsp代码:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.sql.* "%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title></title>
</head>
<body>
<% Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/eco", "root", "vicky");
PreparedStatement ps = con.prepareStatement("select * from registration");
ResultSet rs=ps.executeQuery();
rs.afterLast();
while(rs.previous()){ %>
<p> <%= rs.getString(2) %> </p>
<%} %>
</body>
</html>
我的数据库结构:
ID username email password
1 vikas vikas5@gmail.com 44
2 Aravind Aravind@gmail.com 12
3 rakesh rakesh@gmail.com 123
4 chandra chnadra@gmail.com 123
5 shiva chinthala@gmail.com 12345
6 sai sai@gmail.com 4321
7 ravi ravi@gmail.com 987654
我的输出
ravi
sai
shiva
chandra
rakesh
Aravind
vikas
但我希望输出为
ravi sai shiva
chandra rakesh Aravind
vikas
2条答案
按热度按时间evrscar21#
如果只想获取最后3条记录,则需要更改查询。
必须这样编写嵌套查询:
selectfrom(selectfrom registration order by id)registration,其中rownum<=3 order by rownum desc;
有关更多信息,请参阅以下链接:
https://www.techonthenet.com/oracle/questions/bottom_records.php
https://www.techonthenet.com
这是一个非常好的,非常有用的网站,学习数据库和其他技术。
希望对你有帮助
你也可以这样做这个查询也是工作
按id desc limit 3从注册顺序中选择*;
dgiusagp2#
您需要使用带限制的order by。
Order by ID DESC
根据以下条件按降序排列数据ID
列。LIMIT 3
获取排序数据中的前3行。更改以下内容:
至