SQL中count(1)、count(*) 与 count(列名)的区别详细解释
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class CountExample {
private static final String JDBC_URL = "jdbc:mysql://localhost:3306/yourdatabase";
private static final String JDBC_USER = "yourusername";
private static final String JDBC_PASSWORD = "yourpassword";
public static void main(String[] args) {
try (Connection conn = DriverManager.getConnection(JDBC_URL, JDBC_USER, JDBC_PASSWORD);
Statement stmt = conn.createStatement()) {
// 使用 COUNT(1)
String count1SQL = "SELECT COUNT(1) AS total FROM employees";
ResultSet rs1 = stmt.executeQuery(count1SQL);
if (rs1.next()) {
int total1 = rs1.getInt("total");
System.out.println("Total rows (COUNT(1)): " + total1);
}
// 使用 COUNT(*)
String countAllSQL = "SELECT COUNT(*) AS total FROM employees";
ResultSet rsAll = stmt.executeQuery(countAllSQL);
if (rsAll.next()) {
int totalAll = rsAll.getInt("total");
System.out.println("Total rows (COUNT(*)): " + totalAll);
}
// 使用 COUNT(column)
String countColumnSQL = "SELECT COUNT(department_id) AS total FROM employees";
ResultSet rsColumn = stmt.executeQuery(countColumnSQL);
if (rsColumn.next()) {
int totalColumn = rsColumn.getInt("total");
System.out.println("Total rows (COUNT(department_id)): " + totalColumn);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
- .NET Core系列之MemoryCache 初识
- 007手机一键Root(安机网一键Root) v3.0 官方最新版 一键ROOT您的Android手机
- 12306密码被盗了怎么办?12306密码外泄解决方法
- 12个字的qq网名
- 150M迷你型无线路由器怎么设置?
- 192.168.1.1打不开怎么办?路由器192.168.1.1打不开的原因以及解决办法
- 2011年电子报合订本 电子报 编辑部 中文 PDF版 [84M]
- 2015年1月15日小米新旗舰发布会现场图文直播
- 2016.3.1vivo Xplay5新品发布会现场视频直播 优酷直播
- 2016华为P9发布会视频直播地址 4月15日华为P9国行发布会直播