-
Notifications
You must be signed in to change notification settings - Fork 72
select实战 #130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
select实战 #130
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,9 +2,8 @@ | |
| package com.github.hcsp.sql; | ||
| import java.io.File; | ||
| import java.math.BigDecimal; | ||
| import java.sql.Connection; | ||
| import java.sql.DriverManager; | ||
| import java.sql.SQLException; | ||
| import java.sql.*; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class Sql { | ||
|
|
@@ -71,7 +70,7 @@ public String toString() { | |
| /** | ||
| * 题目1: | ||
| * 查询有多少所有用户曾经买过指定的商品 | ||
| * | ||
| * @param databaseConnection 数据库连接 | ||
| * @param goodsId 指定的商品ID | ||
| * @return 有多少用户买过这个商品 | ||
| */ | ||
|
|
@@ -82,13 +81,22 @@ public String toString() { | |
| // | 2 | | ||
| // +-----+ | ||
| public static int countUsersWhoHaveBoughtGoods(Connection databaseConnection, Integer goodsId) throws SQLException { | ||
| return 0; | ||
| String sql = "select count(distinct user_id) from \"ORDER\" where goods_id = ? ;"; | ||
| try (PreparedStatement preparedStatement = databaseConnection.prepareStatement(sql)) { | ||
| preparedStatement.setInt(1, goodsId); | ||
| ResultSet resultSet = preparedStatement.executeQuery(); | ||
| if (resultSet.next()) { | ||
| return resultSet.getInt(1); | ||
| } else { | ||
| return -1; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * 题目2: | ||
| * 分页查询所有用户,按照ID倒序排列 | ||
| * | ||
| * @param databaseConnection 数据库连接 | ||
| * @param pageNum 第几页,从1开始 | ||
| * @param pageSize 每页有多少个元素 | ||
| * @return 指定页中的用户 | ||
|
|
@@ -100,7 +108,22 @@ public static int countUsersWhoHaveBoughtGoods(Connection databaseConnection, In | |
| // | 1 | zhangsan | tel1 | beijing | | ||
| // +----+----------+------+----------+ | ||
| public static List<User> getUsersByPageOrderedByIdDesc(Connection databaseConnection, int pageNum, int pageSize) throws SQLException { | ||
| return null; | ||
| List<User> userList = new ArrayList<>(); | ||
| String sql = "select * from user order by id desc limit ?,?; "; | ||
| try (PreparedStatement preparedStatement = databaseConnection.prepareStatement(sql)) { | ||
| preparedStatement.setInt(1, (pageNum - 1) * pageSize); | ||
| preparedStatement.setInt(2, pageSize); | ||
| ResultSet resultSet = preparedStatement.executeQuery(); | ||
| while (resultSet.next()) { | ||
| User user = new User(); | ||
| user.id = resultSet.getInt(1); | ||
| user.name = resultSet.getString(2); | ||
| user.tel = resultSet.getString(3); | ||
| user.address = resultSet.getString(4); | ||
| userList.add(user); | ||
| } | ||
| } | ||
| return userList; | ||
| } | ||
|
|
||
| // 商品及其营收 | ||
|
|
@@ -117,7 +140,9 @@ public String toString() { | |
|
|
||
| /** | ||
| * 题目3: | ||
| * @param databaseConnection 数据库连接 | ||
| * 查询所有的商品及其销售额,按照销售额从大到小排序 | ||
| * @return 返回商品及销售额表 | ||
| */ | ||
| // 预期的结果应该如图所示 | ||
| // +----+--------+------+ | ||
|
|
@@ -132,7 +157,22 @@ public String toString() { | |
| // | 3 | goods3 | 20 | | ||
| // +----+--------+------+ | ||
| public static List<GoodsAndGmv> getGoodsAndGmv(Connection databaseConnection) throws SQLException { | ||
| return null; | ||
| List<GoodsAndGmv> goodsAndGmvList = new ArrayList<>(); | ||
| try (PreparedStatement statement = databaseConnection.prepareStatement("select GOODS_ID, GOODS.NAME, sum(GOODS_NUM * GOODS_PRICE) as total\n" + | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| "from `ORDER`\n" + | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| " join GOODS on `ORDER`.GOODS_ID = GOODS.ID\n" + | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| "group by GOODS_ID\n" + | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| "order by total desc")) { | ||
| ResultSet resultSet = statement.executeQuery(); | ||
| while (resultSet.next()) { | ||
| GoodsAndGmv goodsAndGmv = new GoodsAndGmv(); | ||
| goodsAndGmv.goodsId = resultSet.getInt(1); | ||
| goodsAndGmv.goodsName = resultSet.getString(2); | ||
| goodsAndGmv.gmv = resultSet.getBigDecimal(3); | ||
| goodsAndGmvList.add(goodsAndGmv); | ||
| } | ||
| } | ||
| return goodsAndGmvList; | ||
| } | ||
|
|
||
|
|
||
|
|
@@ -151,7 +191,9 @@ public String toString() { | |
|
|
||
| /** | ||
| * 题目4: | ||
| * @param databaseConnection 数据库连接 | ||
| * 查询订单信息,只查询用户名、商品名齐全的订单,即INNER JOIN方式 | ||
| * @return 返回订单信息 | ||
| */ | ||
| // 预期的结果为: | ||
| // +----------+-----------+------------+-------------+ | ||
|
|
@@ -170,12 +212,29 @@ public String toString() { | |
| // | 6 | zhangsan | goods3 | 20 | | ||
| // +----------+-----------+------------+-------------+ | ||
| public static List<Order> getInnerJoinOrders(Connection databaseConnection) throws SQLException { | ||
| return null; | ||
| List<Order> orderList = new ArrayList<>(); | ||
| try (PreparedStatement statement = databaseConnection.prepareStatement("select \"ORDER\".ID, U.NAME, G.NAME, GOODS_NUM * GOODS_PRICE\n" + | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| "from `ORDER`\n" + | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| " join USER U on `ORDER`.USER_ID = U.ID\n" + | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| " join GOODS G on `ORDER`.GOODS_ID = G.ID")) { | ||
| ResultSet resultSet = statement.executeQuery(); | ||
| while (resultSet.next()) { | ||
| Order order = new Order(); | ||
| order.id = resultSet.getInt(1); | ||
| order.userName = resultSet.getString(2); | ||
| order.goodsName = resultSet.getString(3); | ||
| order.totalPrice = resultSet.getBigDecimal(4); | ||
| orderList.add(order); | ||
| } | ||
| } | ||
| return orderList; | ||
| } | ||
|
|
||
| /** | ||
| * 题目5: | ||
| * @param databaseConnection 数据库连接 | ||
| * 查询所有订单信息,哪怕它的用户名、商品名缺失,即LEFT JOIN方式 | ||
| * @return 返回订单信息 | ||
| */ | ||
| // 预期的结果为: | ||
| // +----------+-----------+------------+-------------+ | ||
|
|
@@ -198,7 +257,22 @@ public static List<Order> getInnerJoinOrders(Connection databaseConnection) thro | |
| // | 8 | NULL | NULL | 60 | | ||
| // +----------+-----------+------------+-------------+ | ||
| public static List<Order> getLeftJoinOrders(Connection databaseConnection) throws SQLException { | ||
| return null; | ||
| List<Order> orderList = new ArrayList<>(); | ||
| try (PreparedStatement statement = databaseConnection.prepareStatement("select \"ORDER\".ID, U.NAME, G.NAME, GOODS_NUM * GOODS_PRICE\n" + | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| "from `ORDER`\n" + | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| " left join USER U on `ORDER`.USER_ID = U.ID\n" + | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| " left join GOODS G on `ORDER`.GOODS_ID = G.ID")) { | ||
| ResultSet resultSet = statement.executeQuery(); | ||
| while (resultSet.next()) { | ||
| Order order = new Order(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 你看这个方法是不是和 |
||
| order.id = resultSet.getInt(1); | ||
| order.userName = resultSet.getString(2); | ||
| order.goodsName = resultSet.getString(3); | ||
| order.totalPrice = resultSet.getBigDecimal(4); | ||
| orderList.add(order); | ||
| } | ||
| } | ||
| return orderList; | ||
| } | ||
|
|
||
| // 注意,运行这个方法之前,请先运行mvn initialize把测试数据灌入数据库 | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不应使用 '.*' 形式的导入 - java.sql.* 。