-
Notifications
You must be signed in to change notification settings - Fork 72
practise-select-sql #171
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
base: master
Are you sure you want to change the base?
practise-select-sql #171
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 { | ||
|
|
@@ -82,7 +81,15 @@ public String toString() { | |
| // | 2 | | ||
| // +-----+ | ||
| public static int countUsersWhoHaveBoughtGoods(Connection databaseConnection, Integer goodsId) throws SQLException { | ||
| return 0; | ||
| try (PreparedStatement preparedStatement = databaseConnection.prepareStatement("select count(distinct user_id) from \"ORDER\" where goods_id = ? ")) { | ||
| preparedStatement.setInt(1, goodsId); | ||
| try (ResultSet resultSet = preparedStatement.executeQuery()) { | ||
| while (resultSet.next()) { | ||
| return resultSet.getInt(1); | ||
| } | ||
| } | ||
| return -1; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -100,7 +107,25 @@ 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; | ||
| try (PreparedStatement preparedStatement = databaseConnection.prepareStatement("select * from user order by id desc limit(?-1)*?,?")) { | ||
| preparedStatement.setInt(1, pageNum); | ||
| preparedStatement.setInt(2, pageSize); | ||
| preparedStatement.setInt(3, pageSize); | ||
|
|
||
| List<User> list; | ||
| try (ResultSet resultSet = preparedStatement.executeQuery()) { | ||
| list = new ArrayList<>(); | ||
| 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); | ||
| list.add(user); | ||
| } | ||
| } | ||
| return list; | ||
| } | ||
| } | ||
|
|
||
| // 商品及其营收 | ||
|
|
@@ -132,7 +157,23 @@ public String toString() { | |
| // | 3 | goods3 | 20 | | ||
| // +----+--------+------+ | ||
| public static List<GoodsAndGmv> getGoodsAndGmv(Connection databaseConnection) throws SQLException { | ||
| return null; | ||
| try (PreparedStatement preparedStatement = databaseConnection.prepareStatement("select goods.id as id,name,sum(goods_num*goods_price) as gmv from goods\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 `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.
|
||
| "on goods.id = `order`.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 gmv desc")) { | ||
| List<GoodsAndGmv> list = new ArrayList<>(); | ||
| try (ResultSet resultSet = preparedStatement.executeQuery()) { | ||
| while (resultSet.next()) { | ||
| GoodsAndGmv goodsAndGmv = new GoodsAndGmv(); | ||
| goodsAndGmv.goodsId = resultSet.getInt(1); | ||
| goodsAndGmv.goodsName = resultSet.getString(2); | ||
| goodsAndGmv.gmv = resultSet.getBigDecimal(3); | ||
| list.add(goodsAndGmv); | ||
| } | ||
| return list; | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
@@ -170,7 +211,24 @@ public String toString() { | |
| // | 6 | zhangsan | goods3 | 20 | | ||
| // +----------+-----------+------------+-------------+ | ||
| public static List<Order> getInnerJoinOrders(Connection databaseConnection) throws SQLException { | ||
| return null; | ||
| try (PreparedStatement preparedStatement = databaseConnection.prepareStatement("select `order`.id,user.name,goods.name,goods_price * goods_num as total_price 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\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.
|
||
| "on goods.id = `order`.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.
|
||
| "join user\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.
|
||
| "on user.id = `order`.user_id")) { | ||
| try (ResultSet resultSet = preparedStatement.executeQuery()) { | ||
| List<Order> list = new ArrayList<>(); | ||
| 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); | ||
| list.add(order); | ||
| } | ||
| return list; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -198,9 +256,27 @@ public static List<Order> getInnerJoinOrders(Connection databaseConnection) thro | |
| // | 8 | NULL | NULL | 60 | | ||
| // +----------+-----------+------------+-------------+ | ||
| public static List<Order> getLeftJoinOrders(Connection databaseConnection) throws SQLException { | ||
| return null; | ||
| try (PreparedStatement preparedStatement = databaseConnection.prepareStatement("select `order`.id,user.name,goods.name,goods_price * goods_num as total_price 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 goods\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.
|
||
| "on goods.id = `order`.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.
|
||
| "left join user\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.
|
||
| "on user.id = `order`.user_id")) { | ||
| try (ResultSet resultSet = preparedStatement.executeQuery()) { | ||
| List<Order> list = new ArrayList<>(); | ||
| 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); | ||
| list.add(order); | ||
| } | ||
| return list; | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| // 注意,运行这个方法之前,请先运行mvn initialize把测试数据灌入数据库 | ||
| public static void main(String[] args) throws SQLException { | ||
| File projectDir = new File(System.getProperty("basedir", System.getProperty("user.dir"))); | ||
|
|
||
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.* 。