## Mysql數據庫中查詢某表中第二大的數據
> leetcode記錄
```
編寫一個 SQL 查詢,獲取 Employee?表中第二高的薪水(Salary)?。
+----+--------+
| Id | Salary |
+----+--------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
+----+--------+
例如上述?Employee?表,SQL查詢應該返回?200 作為第二高的薪水。如果不存在第二高的薪水,那么查詢應返回 null。
+---------------------+
| SecondHighestSalary |
+---------------------+
| 200 |
+---------------------+
```
```mysql
# 1. 查詢出最大的記錄,然后查詢剩下記錄中比該記錄小的最大數據記錄
select max(Salary) as SecondHighestSalary
from Employee where Salary < (select max(Salary) from Employee);
# 2. 先取得最大的,然后not in 最大的那個,在剩下的取最大的就是第二個。
select max(Salary) from Employee where Salary not in (select max(Salary) from Employee);
# 3. 使用limit offset
select Salary from Employee order by Salary limit 1,1;
select Salary from Employee order by limit 1 offset 1
```
## MySQL查找重復的電子郵箱
重復的電子郵箱存在多次。要計算每封電子郵件的存在次數,我們可以使用以下代碼
```mysql
select Email, count(Email) as num
from Person
group by Email;
```
```
| Email | num |
|---------|-----|
| a@b.com | 2 |
| c@d.com | 1 |
```
向 `GROUP BY` 添加條件的一種更常用的方法是使用 `HAVING` 子句,該子句更為簡單高效。
```mysql
select Email
from Person
group by Email
having count(Email) > 1;
```
知道使用group by和having。還需要記得優先順序。where>group by>having>order by
相比于執行速度來說,下面的更快
```mysql
select distinct a.Email from Person a, Person b where a.Email=b.Email and a.Id <> b.Id;
```
- PHP獲取客戶端瀏覽器信息和版本
- PHP獲取客戶端操作系統信息
- 無限級分類
- git使用
- 權限檢測思路
- Vue學習
- 遇到的一些問題
- PHP的編碼思維和技巧
- mysql復習
- tp5
- ThinkPHP5.x 公共函數
- TP5登錄注冊
- TP5使用模板繼承
- ThinkPHP5.1 清除緩存
- thinkphp5實現安裝程序
- 安全
- tp中實現跨域代碼
- ThinkPHP5.1配合pjax實現菜單欄無刷新跳轉
- 獲取數據庫版本和數據庫大小
- 模型的基本CURD操作
- 商品spu
- 全局異常處理類
- ExceptionHandler
- BaseException
- PHP函數之error_reporting(E_ALL ^ E_NOTICE)詳細說明
- 微信小程序
- wx:for
- tp6
- 分離的一些模塊
- session開啟
- Spring
- 依賴注入
- 數據結構
- 二叉樹
- js獲取地址欄變量
- PHP設計模式
- 面向對象
- PHP1
- PHP性能優化
- Java學習
- static關鍵字
- 多態
- 接口、階乘
- 大佬給的面試題
- 訪問量為5000萬的博客系統設計
- PHP可變參數
- Nginx的配置案例
- 求數組中的最大值,并返回數組索引
- PHP面試方向
- PHP數組工具類ArrUtil
- 字符串工具類StrUtil
- PHP使用curl發送請求
- mysql
- PHP上傳base64圖片處理函數
- webstorm小程序常用配置
- 郵箱正則表達式
- leetcode mysql記錄
- 函數庫