2018-11-11-weekly
阿新 • • 發佈:2018-11-12
Algorithm
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();