板凳-------Mysql cookbook學習 (十一--------6)

https://blog.csdn.net/weixin_43236925/article/details/146382981
清晰易懂的 PHP 安裝與配置教程

12.6 查找每組行中含有最大或最小值的行

mysql> set @max_price = (select max(price) from painting);
Query OK, 0 rows affected (0.01 sec)mysql> select artist.name, painting.title, painting.price-> from artist inner join painting-> on painting.a_id = artist.a_id-> where painting.price = @max_price;
+----------+-----------+-------+
| name     | title     | price |
+----------+-----------+-------+
| Da Vinci | Mona Lisa |    87 |
+----------+-----------+-------+
1 row in set (0.00 sec)mysql> create table tmpp select max(price) as max_price from painting;
Query OK, 1 row affected (0.07 sec)
Records: 1  Duplicates: 0  Warnings: 0mysql> select artist.name, painting.title, painting.price-> from artist inner join painting inner join tmpp-> on painting.a_id = artist.a_id-> and painting.price = tmpp.max_price;
+----------+-----------+-------+
| name     | title     | price |
+----------+-----------+-------+
| Da Vinci | Mona Lisa |    87 |
+----------+-----------+-------+
1 row in set (0.00 sec)mysql> create table tmpp1-> select a_id, max(price) as max_price from painting group by a_id;
Query OK, 3 rows affected (0.04 sec)
Records: 3  Duplicates: 0  Warnings: 0mysql> select artist.name, painting.title, painting.price-> from artist inner join painting inner join tmpp1-> on painting.a_id = artist.a_id-> and painting.a_id = tmpp1.a_id-> and painting.price = tmpp1.max_price;
+----------+-------------------+-------+
| name     | title             | price |
+----------+-------------------+-------+
| Da Vinci | Mona Lisa         |    87 |
| Van Gogh | The Potato Eaters |    67 |
| Renoir   | Les Deux Soeurs   |    64 |
+----------+-------------------+-------+
3 rows in set (0.00 sec)mysql> select artist.name, painting.title, painting.price-> from artist inner join painting inner join-> (select a_id, max(price) as max_price from painting group by a_id)-> as tmpp1-> on painting.a_id = artist.a_id-> and painting.a_id = tmpp1.a_id-> and painting.price = tmpp1.max_price;
+----------+-------------------+-------+
| name     | title             | price |
+----------+-------------------+-------+
| Da Vinci | Mona Lisa         |    87 |
| Van Gogh | The Potato Eaters |    67 |
| Renoir   | Les Deux Soeurs   |    64 |
+----------+-------------------+-------+
3 rows in set (0.00 sec)mysql> select p1.a_id, p1.title, p1.price-> from painting as p1 left join painting as p2-> on p1.a_id = p2.a_id and p1.price < p2.price-> where p2.a_id is null;
+------+-------------------+-------+
| a_id | title             | price |
+------+-------------------+-------+
|    1 | Mona Lisa         |    87 |
|    3 | The Potato Eaters |    67 |
|    4 | Les Deux Soeurs   |    64 |
+------+-------------------+-------+
3 rows in set (0.00 sec)mysql> select artist.name, p1.title, p1.price-> from painting as p1 left join painting as p2-> on p1.a_id = p2.a_id and p1.price < p2.price-> inner join artist on p1.a_id = artist.a_id-> where p2.a_id is null;
+----------+-------------------+-------+
| name     | title             | price |
+----------+-------------------+-------+
| Da Vinci | Mona Lisa         |    87 |
| Van Gogh | The Potato Eaters |    67 |
| Renoir   | Les Deux Soeurs   |    64 |
+----------+-------------------+-------+
3 rows in set (0.00 sec)mysql> DROP TABLE IF EXISTS driver_log;
Query OK, 0 rows affected (0.05 sec)mysql> #@ _CREATE_TABLE_
mysql> CREATE TABLE driver_log-> (->   rec_id    INT UNSIGNED NOT NULL AUTO_INCREMENT,->   name      VARCHAR(20) NOT NULL,->   trav_date DATE NOT NULL,->   miles     INT NOT NULL,->   PRIMARY KEY (rec_id)-> );
Query OK, 0 rows affected (0.05 sec)mysql> #@ _CREATE_TABLE_
mysql>
mysql> INSERT INTO driver_log (name,trav_date,miles)->   VALUES->     ('Ben','2006-08-30',152),->     ('Suzi','2006-08-29',391),->     ('Henry','2006-08-29',300),->     ('Henry','2006-08-27',96),->     ('Ben','2006-08-29',131),->     ('Henry','2006-08-26',115),->     ('Suzi','2006-09-02',502),->     ('Henry','2006-09-01',197),->     ('Ben','2006-09-02',79),->     ('Henry','2006-08-30',203)-> ;
Query OK, 10 rows affected (0.01 sec)
Records: 10  Duplicates: 0  Warnings: 0mysql>
mysql> SELECT * FROM driver_log;
+--------+-------+------------+-------+
| rec_id | name  | trav_date  | miles |
+--------+-------+------------+-------+
|      1 | Ben   | 2006-08-30 |   152 |
|      2 | Suzi  | 2006-08-29 |   391 |
|      3 | Henry | 2006-08-29 |   300 |
|      4 | Henry | 2006-08-27 |    96 |
|      5 | Ben   | 2006-08-29 |   131 |
|      6 | Henry | 2006-08-26 |   115 |
|      7 | Suzi  | 2006-09-02 |   502 |
|      8 | Henry | 2006-09-01 |   197 |
|      9 | Ben   | 2006-09-02 |    79 |
|     10 | Henry | 2006-08-30 |   203 |
+--------+-------+------------+-------+
10 rows in set (0.00 sec)mysql> select name, trav_date, miles-> from driver_log-> order by name, trav_date;
+-------+------------+-------+
| name  | trav_date  | miles |
+-------+------------+-------+
| Ben   | 2006-08-29 |   131 |
| Ben   | 2006-08-30 |   152 |
| Ben   | 2006-09-02 |    79 |
| Henry | 2006-08-26 |   115 |
| Henry | 2006-08-27 |    96 |
| Henry | 2006-08-29 |   300 |
| Henry | 2006-08-30 |   203 |
| Henry | 2006-09-01 |   197 |
| Suzi  | 2006-08-29 |   391 |
| Suzi  | 2006-09-02 |   502 |
+-------+------------+-------+
10 rows in set (0.00 sec)mysql> create table tmpp2-> select name, max(trav_date) as trav_date-> from driver_log group by name;
Query OK, 3 rows affected (0.04 sec)
Records: 3  Duplicates: 0  Warnings: 01. 第一條語句:用外部表tmpp2進行內連接
sql
SELECT driver_log.name, driver_log.trav_date, driver_log.miles
FROM driver_log 
INNER JOIN tmpp2  -- tmpp2是一個已存在的外部表ON driver_log.name = tmpp2.name AND driver_log.trav_date = tmpp2.trav_date
ORDER BY driver_log.name;?	tmpp2的性質:這里的tmpp2是一個預先存在的外部表(可能是手動創建的臨時表或永久表),里面存儲了一些name和trav_date的數據。
?	連接邏輯:只返回driver_log中與tmpp2表中完全匹配(name和trav_date都相同)的記錄。
?	適用場景:當你需要篩選driver_log中符合某個預設條件(即tmpp2表中定義的特定name和trav_date組合)的記錄時使用。例如:tmpp2可能存儲了 “需要重點檢查的司機和日期”。
2. 第二條語句:用子查詢動態生成tmpp2
sql
SELECT driver_log.name, driver_log.trav_date, driver_log.miles
FROM driver_log 
INNER JOIN (-- 子查詢:動態生成每個司機最新的出行日期SELECT name, max(trav_date) as trav_date FROM driver_log GROUP BY name
) AS tmpp2ON driver_log.name = tmpp2.name AND driver_log.trav_date = tmpp2.trav_date
ORDER BY driver_log.name;?	tmpp2的性質:這里的tmpp2是動態生成的子查詢結果,邏輯是 “對driver_log按司機分組,獲取每個司機最新的出行日期(max(trav_date))”。
?	連接邏輯:只返回driver_log中每個司機的最新出行記錄(因為tmpp2存儲的是每個司機的最大日期)。
?	適用場景:需要從driver_log中篩選每個司機的 “最新一條記錄” 時使用(例如:查詢每個司機最近一次出行的里程)。
核心區別總結
維度	第一條語句(外部表tmpp2)	第二條語句(子查詢生成tmpp2)
tmpp2的來源	外部預設表(內容固定,非動態生成)	子查詢動態生成(內容由driver_log數據決定)
連接的目的	篩選符合預設條件(tmpp2中的name+date)的記錄	篩選每個司機的最新出行記錄(max(trav_date))
結果的決定因素	依賴tmpp2表的預設內容	依賴driver_log自身的最大日期數據
靈活性	低(tmpp2內容變更需手動修改)	高(自動適應driver_log數據變化)

12.7 計算隊伍排名

mysql> select team , wins, losses from standings1-> order by wins-losses desc;
+-------------+------+--------+
| team        | wins | losses |
+-------------+------+--------+
| Winnipeg    |   37 |     20 |
| Crookston   |   31 |     25 |
| Fargo       |   30 |     26 |
| Grand Forks |   28 |     26 |
| Devils Lake |   19 |     31 |
| Cavalier    |   15 |     32 |
+-------------+------+--------+
6 rows in set (0.01 sec)mysql> set @w1_diff = (select max(wins-losses) from standings1);
Query OK, 0 rows affected (0.00 sec)mysql> select team, wins as w, losses as L,-> wins/(wins+losses) as pct,-> (@w1_diff - (wins-losses)) / 2 as gb-> from standings1-> order by wins-losses desc, pct desc;
+-------------+------+------+--------+---------+
| team        | w    | L    | pct    | gb      |
+-------------+------+------+--------+---------+
| Winnipeg    |   37 |   20 | 0.6491 |  0.0000 |
| Crookston   |   31 |   25 | 0.5536 |  5.5000 |
| Fargo       |   30 |   26 | 0.5357 |  6.5000 |
| Grand Forks |   28 |   26 | 0.5185 |  7.5000 |
| Devils Lake |   19 |   31 | 0.3800 | 14.5000 |
| Cavalier    |   15 |   32 | 0.3191 | 17.0000 |
+-------------+------+------+--------+---------+
6 rows in set (0.00 sec)mysql> CREATE TABLE firstplace (->     half VARCHAR(20) NOT NULL,->     division VARCHAR(50) NOT NULL,->     w1_diff INT NOT NULL,->     PRIMARY KEY (half, division)-> );
Query OK, 0 rows affected (0.03 sec)mysql> SELECT->     w1.half,->     w1.division,->     w1.team,->     w1.wins AS w,->     w1.losses AS L,->     TRUNCATE(w1.wins / (w1.wins + w1.losses), 3) AS pct,->     IF(->         fp.w1_diff = w1.wins - w1.losses,->         '-',->         TRUNCATE((fp.w1_diff - (w1.wins - w1.losses)) / 2, 1)->     ) AS gb-> FROM standings2 AS w1-> INNER JOIN firstplace AS fp->     ON w1.half = fp.half AND w1.division = fp.division-> ORDER BY->     w1.half,->     w1.division,->     w1.wins - w1.losses DESC,->     pct DESC;
Empty set (0.01 sec)mysql> SELECT->     w1.half,->     w1.division,->     w1.team,->     w1.wins AS w,->     w1.losses AS l,->     TRUNCATE(w1.wins / (w1.wins + w1.losses), 3) AS pct,->     -- 計算與榜首的差距(gb)->     IF(->         w1.wins - w1.losses = fp.max_diff,  -- 若當前球隊是榜首->         '-',  -- 榜首的gb為'-'->         TRUNCATE((fp.max_diff - (w1.wins - w1.losses)) / 2, 1)  -- 其他球隊的gb->     ) AS gb-> FROM standings2 AS w1-> -- 子查詢獲取每個分區、半程的最大勝負差(即榜首球隊的勝負差)-> INNER JOIN (->     SELECT->         half,->         division,->         MAX(wins - losses) AS max_diff  -- 最大勝負差 = 榜首球隊的勝負差->     FROM standings2->     GROUP BY half, division-> ) AS fp-> ON w1.half = fp.half AND w1.division = fp.division-> -- 排序:按半程、分區,再按勝負差(降序)、勝率(降序)-> ORDER BY->     w1.half,->     w1.division,->     (w1.wins - w1.losses) DESC,->     pct DESC;
+------+----------+-----------------+------+------+-------+------+
| half | division | team            | w    | l    | pct   | gb   |
+------+----------+-----------------+------+------+-------+------+
|    1 | Eastern  | St. Paul        |   24 |   18 | 0.571 | -    |
|    1 | Eastern  | Thunder Bay     |   18 |   24 | 0.428 | 6.0  |
|    1 | Eastern  | Duluth-Superior |   17 |   24 | 0.414 | 6.5  |
|    1 | Eastern  | Madison         |   15 |   27 | 0.357 | 9.0  |
|    1 | Western  | Winnipeg        |   29 |   12 | 0.707 | -    |
|    1 | Western  | Sioux City      |   28 |   14 | 0.666 | 1.5  |
|    1 | Western  | Fargo-Moorhead  |   21 |   21 | 0.500 | 8.5  |
|    1 | Western  | Sioux Falls     |   15 |   27 | 0.357 | 14.5 |
|    2 | Eastern  | Duluth-Superior |   22 |   20 | 0.523 | -    |
|    2 | Eastern  | St. Paul        |   21 |   21 | 0.500 | 1.0  |
|    2 | Eastern  | Madison         |   19 |   23 | 0.452 | 3.0  |
|    2 | Eastern  | Thunder Bay     |   18 |   24 | 0.428 | 4.0  |
|    2 | Western  | Fargo-Moorhead  |   26 |   16 | 0.619 | -    |
|    2 | Western  | Winnipeg        |   24 |   18 | 0.571 | 2.0  |
|    2 | Western  | Sioux City      |   22 |   20 | 0.523 | 4.0  |
|    2 | Western  | Sioux Falls     |   16 |   26 | 0.380 | 10.0 |
+------+----------+-----------------+------+------+-------+------+
16 rows in set (0.01 sec)
關鍵修改說明
用子查詢替代firstplace表:
通過 SELECT half, division, MAX(wins - losses) AS max_diff FROM standings2 GROUP BY half, division 動態計算每個分區、半程的榜首球隊勝負差(無需手動創建firstplace表),避免了 “表不存在” 的錯誤。
gb字段計算邏輯:
若球隊的勝負差(wins - losses)等于榜首的最大勝負差(max_diff),則gb為'-'(表示該隊是榜首)。
否則,gb為(榜首勝負差 - 該隊勝負差)/ 2(這是體育排名中計算 “場次差距” 的標準公式)。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/913769.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/913769.shtml
英文地址,請注明出處:http://en.pswp.cn/news/913769.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

ECS由淺入深第四節:ECS 與 Unity 傳統開發模式的結合?混合架構的藝術

ECS由淺入深第一節 ECS由淺入深第二節 ECS由淺入深第三節 ECS由淺入深第四節 ECS由淺入深第五節 盡管 ECS 帶來了顯著的性能和架構優勢&#xff0c;但在實際的 Unity 項目中&#xff0c;完全摒棄 GameObject 和 MonoBehaviour 往往是不現實的。Unity 引擎本身的大部分功能&…

Mac關閉觸控板

打開 “有鼠標或無線觸控板時忽略內建觸控板”選項即可 參考&#xff1a;Mac如何關閉觸控板防止誤觸&#xff1f;內置的設置就可以達成 - Mac天空

Python:Rich 終端富文本與界面樣式工具庫

??? 1、簡述 Rich 是一個強大的 Python 庫,用于在終端中呈現富文本和精美的格式,讓命令行界面(CLI)應用擁有現代、美觀的輸出效果。本文將深入介紹 Rich 的核心功能,并通過一系列實際示例展示其強大能力。 Rich 由 Will McGugan 開發,主要特點包括: 豐富的文本樣式:支…

深入解析享元模式:通過共享技術高效支持大量細粒度對象

深入解析享元模式&#xff1a;通過共享技術高效支持大量細粒度對象 &#x1f31f; 嗨&#xff0c;我是IRpickstars&#xff01; &#x1f30c; 總有一行代碼&#xff0c;能點亮萬千星辰。 &#x1f50d; 在技術的宇宙中&#xff0c;我愿做永不停歇的探索者。 ? 用代碼丈量世…

Docker高級管理

一、Docker 容器的網絡模式 當項目大規模使用 Docker 時&#xff0c;容器通信的問題也就產生了。要解決容器通信問題&#xff0c;必須先了解很多關于網絡的知識。Docker 的網絡模式非常豐富&#xff0c;可以滿足不同容器的通信要求&#xff0c;下表列出了這些網絡模式的主要信息…

ABP VNext + Tye:本地微服務編排與調試

ABP VNext Tye&#xff1a;本地微服務編排與調試 &#x1f680; &#x1f4da; 目錄ABP VNext Tye&#xff1a;本地微服務編排與調試 &#x1f680;TL;DR ?一、環境與依賴 &#x1f6e0;?二、核心配置詳解 &#x1f680;1. 主配置 tye.yaml三、多環境文件 &#x1f331;&am…

Vue響應式原理一:認識響應式邏輯

核心思想&#xff1a;當數據發生變化時&#xff0c;依賴該數據的代碼能夠自動重新執行Vue中的應用&#xff1a;在data或ref/reactive中定義的數據&#xff0c;當數據變化時template會自動更新template的本質&#xff1a; 是render()函數, 用變化之后的數據重新執行render()函數…

Redis:分組與設備在 Redis 中緩存存儲設計

一、緩存存儲結構設計 分組與設備的映射關系&#xff08;使用 Set 結構&#xff09;&#xff1a; 鍵格式&#xff1a;采用 group:{groupId}:devices 的格式作為 Redis 中 Set 的鍵&#xff0c;例如 group:1:devices 就代表了分組 ID 為 1 的分組所關聯的設備集合。值內容&#…

Leetcode 3605. Minimum Stability Factor of Array

Leetcode 3605. Minimum Stability Factor of Array 1. 解題思路2. 代碼實現 題目鏈接&#xff1a;3605. Minimum Stability Factor of Array 1. 解題思路 這一題的核心思路是二分法&#xff0c;本質上就是我們給定一個常數kkk&#xff0c;然后考察是否存在一個構造使得能夠…

編譯安裝的Mysql5.7報“Couldn‘t find MySQL server (mysqld_safe)“的原因 筆記250709

編譯安裝的Mysql5.7報"Couldn’t find MySQL server (mysqld_safe)"的原因 筆記250709 MySQL 的安裝路徑與配置文件&#xff08;如 my.cnf 或 mysql.server&#xff09;中指定的 basedir 不一致。 mysqld_safe 文件實際位置與系統查找路徑不匹配&#xff08;常見于自…

在 Ubuntu 下配置 oh-my-posh —— 普通用戶 + root 各自使用獨立主題(共享可執行)

&#x1f9e9; 在 Ubuntu 下配置 oh-my-posh —— 普通用戶 root 各自使用獨立主題&#xff08;共享可執行&#xff09;? 目標說明普通用戶 使用 tokyonight_storm 主題 root 用戶 使用 1_shell 主題 共用全局路徑下的 oh-my-posh 可執行文件 正確加載 Homebrew 到環境變量中…

Spring Boot 項目中的多數據源配置

關鍵詞&#xff1a;Spring Boot、多數據源配置、MySQL、SQL Server、Oracle、動態切換 ? 摘要 在實際企業級開發中&#xff0c;一個 Spring Boot 項目可能需要連接多個數據庫&#xff0c;比如 MySQL、SQL Server 和 Oracle。不同的業務模塊可能依賴不同的數據源&#xff0c;這…

MATLAB/Simulink電機控制仿真代做 同步異步永磁直驅磁阻雙饋無刷

以下是針對 MATLAB/Simulink 電機控制仿真 的系統性解決方案&#xff0c;涵蓋 同步電機、異步電機、永磁電機、直驅電機、磁阻電機、雙饋電機、無刷直流電機&#xff08;BLDC&#xff09; 的建模與控制策略實現&#xff0c;支持代做服務的技術細節和代碼示例。一、電機建模與仿…

限流算法深度探索:從理論到實踐的生產級避坑指南

凌晨3點&#xff0c;監控警報刺耳地尖叫著。我盯著屏幕上垂直下跌的服務可用性曲線&#xff0c;意識到那個被忽視的限流配置項終于引爆了——每秒1000次的支付請求正像洪水般沖垮我們的系統。這次事故讓我深刻理解&#xff1a;限流不是可選項&#xff0c;而是分布式系統的生存法…

企業級后臺管理系統的困境與飛算 JavaAI 的破局之道

企業級后臺管理系統如 CRM&#xff08;客戶關系管理系統&#xff09;、ERP&#xff08;企業資源計劃系統&#xff09;已成為支撐企業高效運轉的核心骨架。它們如同企業的 “神經中樞”&#xff0c;串聯起客戶數據、財務信息、供應鏈流程等關鍵環節&#xff0c;為決策制定、業務…

快速上手百寶箱搭建知識闖關游戲助手

引言&#xff1a;讓學習更有趣&#xff0c;AI 賦能知識闖關新體驗 1.在信息爆炸的時代&#xff0c;傳統的填鴨式教學方式已難以滿足現代用戶對高效、個性化和趣味化學習的需求。越來越多的學習者傾向于通過互動性強、參與感十足的方式獲取知識。在此背景下&#xff0c;游戲化學…

【YOLOv11-目標檢測】目標檢測數據格式(官方說明)

原文鏈接&#xff1a; https://docs.ultralytics.com/datasets/detect/ 寫在前面 訓練一個魯棒且準確的目標檢測模型需要一個全面的數據集。本文介紹&#xff1a;與Ultralytics YOLO模型兼容的各種數據集格式&#xff0c;并深入解析了它們的結構、使用方法以及如何在不同的格…

yolo8實現目標檢測

?步驟一&#xff1a;安裝 PyTorch&#xff08;M1 專用&#xff09;# 推薦使用官方 MPS 后端&#xff08;Apple Metal 加速&#xff09; pip install torch torchvision torchaudio確認是否使用了 Apple MPS&#xff1a;import torch print(torch.backends.mps.is_available()…

安全管理協議(SMP):配對流程、密鑰生成與防中間人攻擊——藍牙面試核心考點精解

一、SMP 核心知識點高頻考點解析1.1 SMP 在藍牙安全體系中的定位考點&#xff1a;SMP 的功能與協議棧位置解析&#xff1a; SMP&#xff08;Security Manager Protocol&#xff0c;安全管理協議&#xff09;是藍牙核心規范中負責設備配對、密鑰生成與安全連接的關鍵協議&#x…

U盤實現——U 盤類特殊命令

文章目錄 U 盤類特殊命令U 盤的命令封包命令階段數據階段狀態階段get max luninquiry(0x12)read format capacities(0x23)read capacity(0x25)mode sense(0x1a)test unit ready(0x00)read(10) 0x28write(10) 0x2aU 盤類特殊命令 U 盤的命令封包 命令階段 命令階段主要由主機通…