2018-11-11-weekly

Algorithm

601. 体育馆的人流量

  • What X 市建了一个新的体育馆,每日人流量信息被记录在这三列信息中:序号 (id)、日期 (date)、 人流量 (people)。请编写一个查询语句,找出高峰期时段,要求连续三天及以上,并且每天人流量均不少于100。

  • How 此题可以分成三个表s1,s2,s3的组合判断,以id+1,id+2或者id+1,id-1或者id-1,id-2为条件做全表查询,相当于s1 s2 s3 的顺序三个连续的.

  • Key Codes

# Write your MySQL query statement below
SELECT DISTINCT
  s1.id,
  s1.date,
  s1.people
FROM
  stadium AS s1,
  stadium AS s2,
  stadium AS s3
WHERE (
    (s1.id + 1 = s2.id
      AND s1.id + 2 = s3.id)
    OR (s1.id - 1 = s2.id
      AND s1.id + 1 = s3.id)
    OR (s1.id - 2 = s2.id
      AND s1.id - 1 = s3.id)
  )
  AND s1.people >= 100
  AND s2.people >= 100
  AND s3.people >= 100
ORDER BY s1.id

Review

如何将一群陌生人变为一个团队!

Tip

jdbc编程步骤:

1.加载驱动:
Class.forName(“com.mysql.jdbc.Driver”); //加载MySql数据库(或者通过new)
new com.mysql.jdbc.Driver();
2.获取Connection对象:
Connection conn = DriverManger.getConntection(“jdbc:mysql://localhost:8080/db_name”);
Connection conn = DriverManager.getConnection(“jdbc:mysql://localhost:8080/db_name”,”username”,”password”);
3.获取状态
Statement stmt = conn.createStatement();
Statement stmt = conn.createStatement(resultSetType,resultSetConcurrency); 
[注]resultSetType:结果集类型,有三种ResultSet.TYPE_FORWARD_ONLY,ResultSet.TYPE_SCROLL_INSENSITIVE(对滚动不敏感),ResultSet.TYPE_SCROLL_SENSITIVE(对滚动敏感);resultSetConcurrency:并发类型,有2种ResultSet.CONCUR_READ_ONLY(并发操作是只读,对所有数据库支持),ResultSet.CONCUR_UPDATABLE(并发操作时可更新,有些 有些数据库不支持)
4.执行SQL语句
stmt.execute(“”);
stmt.executeQuery(“”);//执行查询语句
stmt.executeUpdate(“”);//执行更新语句
5.获取ResultSet结果集
ResultSet result = stmt.executeQuery(“”);
while (result.next()) { } //循环获取
6.关闭 (最后的最先关)
result.close();
stmt.close();
conn.close();

Share

双11背后的全球数字经济“操作系统”

《阿里巴巴Java开发规约》插件使用详细指南

原文地址:https://www.cnblogs.com/lanqingyu/p/9948955.html