ORDER BY子句用于按其使用关键字的顺序排列输出集的内容,ASC代表升序,DESC代表降序,如果您不提及其中任何一个,则默认情况下内容将按升序排列。
Order By - 语法
以下是ORDER BY子句的语法-
SELECT * FROM table_name ORDER BY column_name ASC|DESC.
Order By - 命令行示例
假设无涯教程在数据库中有一个名为Employees的表,其中包含以下记录-
ID |NAME |SALARY |LOCATION ------------------------------------------------------------------------------ 1 |Amit |30000 |Vijayawada 2 |Kalyan |40000 |Vishakhapatnam 3 |Renuka |50000 |Delhi 4 |Learnfk |15000 |Vijayawada 5 |Trupthi |45000 |Kochin 6 |Suchatra |33000 |Vijayawada 7 |Rahul |39000 |Lucknow
以下查询根据Employee的名称以升序排列表的内容。
ij> SELECT * FROM Employees ORDER BY Name;
这将生成以下输出-
ID |NAME |SALARY |LOCATION --------------------------------------------------------------- 1 |Amit |30000 |Hyderabad 4 |Learnfk |15000 |Mumbai 2 |Kalyan |40000 |Vishakhapatnam 7 |Rahul |39000 |Lucknow 3 |Renuka |50000 |Delhi 6 |Suchatra |33000 |Pune 5 |Trupthi |45000 |Kochin 7 rows selected
类似地,以下查询根据Employee的薪水以降序排列表的内容-
ij> SELECT * FROM Employees ORDER BY Salary DESC;
这将生成以下输出-
ID |NAME |SALARY |LOCATION --------------------------------------------------------------- 3 |Renuka |50000 |Delhi 5 |Trupthi |45000 |Kochin 2 |Kalyan |40000 |Vishakhapatnam 7 |Rahul |39000 |Lucknow 6 |Suchatra |33000 |Pune 1 |Amit |30000 |Hyderabad 4 |Learnfk |15000 |Mumbai 7 rows selected
Order By - JDBC示例
本节教您如何使用JDBC在Derby中对表的内容进行排序,您可以使用ORDER BY子句和关键字ASC(表示升序)和DSC(表示降序)来按顺序排列记录。
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class SortData { public static void main(String args[]) throws SQLException, ClassNotFoundException { //注册驱动 Class.forName("org.apache.derby.jdbc.EmbeddedDriver"); //创建连接 String URL="jdbc:derby:SampleDB;create=true"; Connection conn=DriverManager.getConnection(URL); //获取Statement对象 Statement stmt=conn.createStatement(); //创建表结构,插入一些测试数据 String query="CREATE TABLE Employees(" + "Id INT NOT NULL GENERATED ALWAYS AS IDENTITY, " + "Name VARCHAR(255), Salary INT NOT NULL, " + "Location VARCHAR(255), " + "PRIMARY KEY (Id))"; String query="INSERT INTO Employees(" + "Name, Salary, Location) VALUES " + "('Amit', 30000, 'Hyderabad'), " + "('Kalyan', 40000, 'Vishakhapatnam'), " + "('Renuka', 50000, 'Delhi'), " + "('Learnfk', 15000, 'Mumbai'), " + "('Trupthi', 45000, 'Kochin'), " + "('Suchatra', 33000, 'Pune'), " + "('Rahul', 39000, 'Lucknow'), " + "('Trupti', 45000, 'Kochin')"; //执行SQL语句 String query="SELECT Location, SUM(Salary) " + "from Employees GROUP BY Location"; ResultSet rs=stmt.executeQuery(query); while(rs.next()) { System.out.println("Salary: "+rs.getString(1)); System.out.println("Location: "+rs.getString(2)); System.out.println(" "); } } }
在执行上述程序时,您将获得以下输出-
Salary: Chennai Location: 43000 Salary: Delhi Location: 50000 Salary: Hyderabad Location: 30000 Salary: Kochin Location: 45000 Salary: Lucknow Location: 39000 Salary: Mumbai Location: 15000 Salary: Pune Location: 33000
参考链接
https://www.learnfk.com/derby/apache-derby-order-by-clause.html
标签:Salary,45000,Employees,无涯,Order,Location,Derby,Kochin,ORDER From: https://blog.51cto.com/u_14033984/7798960