1. 程式人生 > 其它 >mysql 從一組資料中去重並按最大值排序

mysql 從一組資料中去重並按最大值排序

單表查詢

select 表頭,表頭 as 別名 ,表頭(+-*/的運算) from table_a

1.條件查詢

where + 條件
<> , !=不等於
= 等於,也可以表示字串值相等
<,> 小於,大於
>=,<= 大於等於,小於等於
between... and.. 兩個值之間(左小右大,左右都閉),等同於 >=and <=
is null,is not null 為空,不為空 (注意null 不能使用=進行衡量)
and 並且,and 語句優先順序高於or
or 或者 例:select name from table where name=‘張三‘or ‘‘李四‘
not not 表示取非, 可以在is ,in (in 後是具體值,不是區間)中使用
and ,or的優先順序對比

and 優先順序高於or,若希望先執行or 可以使用(),不確定優先,注意使用()

例:查詢工資大於5500,並且編號為10或20的員工

select  
     name
from  
    emp
where  sal>5500 and (deptno=10 or deptno=20)

-- 這裡and 優先順序高於or ,括號的使用可以對內容進行組合,由此實現內查詢
模糊查詢

like 模糊查詢,支援%和_(下劃線)

  • %匹配任意一個字元
  • _代表任意一個字元(一個下劃線佔一個位置)
  • 若要like 含有_的內容,可以使用\進行轉義
- 表示以a開頭的所有字元
表頭  like ‘a%‘

- 表示以a結尾的所有字元
表頭 like ‘%a‘

- 表示表中含有_字元的所有字元
表頭 like ‘%\_%‘

- 表示第二字母是a的所有字元
 表頭 like ‘_a%‘

- 表示倒數第二字母是a的所有字元
 表頭 like ‘%a_‘

 

2、排序

order by 表頭

  • +desc(指定降序)/asc(升序,預設)
  • +列號 如2
  • 排序總是在最後
  • slect ... from ... where .....order by
-- 多個欄位排序  當欄位相等才按後面進行排序
select sal from  emp order by sal desc,ename asc;

-- 瞭解 列號排序
select sal from emp order by 2

 

3.資料處理函式

  • 又稱為單行處理函式
  • 單行處理函式的特徵:一個輸入對應一個輸出
  • 多行處理函式:多行輸入對應一個輸出
  • 常見函式 select function(表頭 ) from table_a 或者 在where 條件中
lower轉換小寫
upper 轉換大寫
substr 取子串(substr(被擷取的字串,起始下標(注下標 從1開始),擷取的長度))
length 取長度
trim 去空格
str_to_data 將字串轉換成日期
data_format 格式化日期
format 設定千分位
round 四捨五入 round(表頭,保留的小數位)
rand() 生成隨機數
ifnull 將null 轉換成一個具體值 ;因為在所有資料中只要有null參與的數學運算結果就是null; 如:ifnull(表頭,0) 如果為null 當做一個值來看
case... when..then..when..then.. else.....end case 表頭 when 條件1 then 執行a when 條件2 then 執行2 else 其他 end
-- substr應用
-- 從 emp 獲取以A開頭的ename
select ename from emp where substr(ename,1,1)=‘A‘;

--  length應用
-- 檢視emp中ename的字元長度
select length(ename) from emp ;


-- null 參與資料計算最終結果一定為null,為了避免這個現象,需要使用ifnull 函式
ifnull(表頭,0) 如果表頭為null 則視為0
select ename,sal+ifnull(comm,0)as salcomm from emp

--case ...when. then... when ... then..else... end 應用
-- 當員工的崗位為sd時,工資上調10%,當工作崗位為ds時,工資上調20%,其他正常,不修改資料庫
select ename,job,
(case job when ‘sd‘ then sal*1.1 when ‘ds‘ then sal*1.2 else sal end)
as newsal 
from 
 emp;

 

  • select ‘sda ‘/ 1000 from emp

4.分組函式(多行處理函式)

  • 需要先分組,才能用 多與group by 一同使用
  • 注意
    • 如果沒有分組,則預設整張表為一組
    • 不需要對null 進行處理,自動忽略null
    • count(*) 與count(具體欄位)的區別
      • count(具體欄位)表示統計該欄位(該列)所有不為null的元素的總和
      • count(*) 這裡按行(因為不存在全為null的行)統計,即統計總行數量
    • __不能__在where條件中使用
    • 所有的分組函式可以聯合使用
  
count 計數
sum 求和
avg 平均數
max 最大值
min 最小值
-- 例項
select min(asl) from emp
select max(asl) from emp
select sum(asl) from emp
select avg(asl) from emp
select count(ename) from emp

--  count(*) 
select count(*) from emp;
-- 表示一共有幾行,即總行數量

select count(表頭) from emp:
-- 即不為null的行的數量

 

5.分組查詢

應用中需要先 進行分組,再對分組後的資料進行操作

select  ...  from  ... group by 表頭

 

  • select 表頭a, 分組函式(表頭b) group by 表頭a;

    在select 語句當中,如果有group by 語句,select 後只能跟:參加分組的欄位,以及分組函式,

    其他一律不能跟(1.沒有意義,2.若在oracle中報錯,相較而言mysql 語法較為鬆散)

  • group by 表頭1,表頭2 兩個欄位聯合分組

-- 應用
-- 找出每個部門,不同工作崗位的最高薪資
-- 技巧 兩個欄位聯合分組
select deptno,jon,max(sal) from emp group by deptno,job

 

  • 使用having對分組後資料進行過濾

效率不高,實際上可以先where出再進行分組,where和having 優先使用having

select deptno,jon,max(sal) from emp group by deptno,job having sal>3000;
或者
select deptno,jon,max(sal) from emp where sal>3000 group by deptno,job ;

-- hvaing + 分組函式 select deptno,jon,max(sal) from emp group by deptno,job having avg(sal)>3000

 

補充

執行順序

select ... from .. where .... group by ...order by..
先執行from ,再者 where ,接著是group by 之後是select 最後我order by..

從某張表中查詢資料,
先經過where 條件篩選出有價值的資料,
對這些有價值的資料進行分組
分組之後可以使用having繼續篩選
select查詢出來
最後排序輸出

 

原文地址:https://www.cnblogs.com/yescarf/p/14092434.html