MySQL查詢——select

来源:https://www.cnblogs.com/aitiknowledge/archive/2019/09/03/11455419.html
-Advertisement-
Play Games

SELECT select的完整語法: 上述如果都有:執行順序from->where->group by->having->order by->limit->select 列的結果顯示 1、去掉重覆的數據:distinct(針對於記錄而言,不是針對於列的數據而言) 2、運算符:+、-、*、/、%(只 ...


SELECT

  select的完整語法:

select col1, col2,...          # 業務查詢的欄位
from table_name            # 選取的哪張表
[where single_conditions]           # single_conditions條件表達式,個體約束(條件)
[[group by column_name1]      # column_name1以哪個欄位名分組
[having group_conditions]]     # group_conditionds條件表達式,分組約束
[order by column_name2]      # column_name2以哪個欄位進行排序
[limit N,M]                         # 執行完之後,跳過N條記錄,選取M條記錄

  上述如果都有:執行順序from->where->group by->having->order by->limit->select

  列的結果顯示  

    1、去掉重覆的數據:distinct(針對於記錄而言,不是針對於列的數據而言)

# 查看員工的職位
select title
from s_emp;

select distinct title 
from s_emp;

# 每個部門下有哪些職位
select dept_id,title 
from s_emp;

select distinct dept_id,title 
from s_emp;# 聯合唯一

    2、運算符:+、-、*、/、%(只列舉一個)

# 查看員工年薪
select salary*12 
from s_emp;
select id,last_name,salary*12 as year_salary 
from s_emp;

# 查看員工當前年薪以及月薪提高100美元後的年薪
select id,last_name,salary*12 old_year_salary,(salary+100)*12 as new_year_salary
from s_emp;

    3、列與列的數據拼接:concat(column_name1, "拼接符", column_name2[, "拼接符"])

# 查看員工基本信息(編號,全名,工資)
select id,concat(first_name,' ',last_name) as name,salary 
from s_emp;

    4、將null值轉換為特定值:ifnull(column_name, 特定值)

# 查看員工工資(沒有工資的顯示為0)
select id,last_name,ifnull(salary,0.00) as salary 
from s_emp;

 

  where conditions

    MySQL的運算符概念及作用:

    主要是根據conditions的條件查詢結果集

    1、比較運算符:=、<、>、>=、<=、!=、<=>(同is null)、<>(同!=)

# 查看擁有白領工資的員工有哪些(12002000select id,last_name,salary 
from s_emp 
where salary>1200 and salary<2000;

# 查看有工資的員工信息
select id,last_name,salary 
from s_emp 
where salary is not null;

select id,last_name,salary 
from s_emp
where salary <> null;# erro

# 查看沒有工資的員工的信息
select id,last_name,salary 
from s_emp 
where salary is null;

select id,last_name,salary 
from s_emp 
where salary <=> null;

select id,last_name,salary 
from s_emp 
where salary != 1200;# null數據未取到

    2、邏輯運算符:and(&&)、or(||)、not

查看41號部門的員工信息並且工資大於1200或者43號部門工資小於2000的員工信息
select id,last_name,salary
from s_emp
where (dept_id=41 and salary>1200) or (dept_id=43 and salary<2000);

    3、在XXX區間:between ... and ...  不在XXX區間:not between ... and ...

查看擁有白領工資的員工有哪些(12002000select id,last_name,salary from s_emp where salary>1200 and salary<2000;
# 下麵是閉區間
select id,last_name,salary 
from s_emp
where salary between 1199 and 2001;        #[1199,2001]

# 不在區間內
select id,last_name,salary 
from s_emp 
where salary not between 1200 and 2000;    #(-&1200or2000+&

    4、在集合中: in ()    不在集合中:not in ()

# 查看41,42,43號部門的員工有哪些
select id,last_name,salary 
from s_emp
where dept_id=41 and dept_id=42 and dept_id=43;
# 和上面的等價
select id,last_name,salary 
from s_emp
where dept_id in(41,42,43);

# 查看不是41,42,43號部門的員工有哪些
select id,last_name,salary 
from s_emp
where dept_id!=41 and dept_id!=42 and dept_id!=43;
# 和上面的等價
select id,last_name,salary 
from s_emp
where dept_id not in(41,42,43)

    6、模糊匹配:like :%:0到多個字元匹配;_:1個字元匹配;[ ]:範圍內的匹配的單個字元;[^ ]:範圍外的匹配的單個字元;   不模糊匹配: not

# 查看職位以VP開頭的員工有哪些
select id,last_name,salary 
from s_emp
where title like 'VP%';

select id,last_name,title 
from s_emp
where title not like 'VP%';

# 查看員工信息,名字以C開頭,並且字元數不小於5個字元
select id,last_name
from s_emp
where last_name like 'C____%';
#查看客戶信息,客戶名稱中包含單引號的客戶
select id,name from s_cutomer
where name like "%'%";

  group by column_name

    涉及的組函數:計數count()、最小值min()、最大值max()、平均值avg()、總和sum()

# 查看員工總數
select count(*) as count_num 
from s_emp;   # 預設分組(以表格為單元)
select count(id) as count_num 
from s_emp;

# 統計有工資的員工個數
select count(salary) as count_num 
from s_emp;
# 查看每個部門的員工個數
select dept_id,count(*) as nums 
from s_emp
group by dept_id;

# 進行分組後,select的結果只能是組的概念,不允許出現個體概念(last_name)
select dept_id,count(*) as nums,last_name 
from s_emp
group by dept_id;   # errro

# 預設以逗號拼接 group_concat(),這個函數很重要
select dept_id,count(*) as nums,group_concat(last_name) 
from s_emp
group by dept_id;

# 查看每個部門薪資大於1200的員工總數(信息)
select dept_id,count(*) nums,group_concat(last_name),group_concat(salary)
from s_emp
where salary > 1200
group by dept_id;

# 查看部門平均薪資
select avg(salary) 
from s_emp 
group by dept_id;

# 查看部門平均薪資>2000員工總數
select dept_id,count(*) nums,avg(salary)
from s_emp
group by dept_id
having avg(salary)>2000;

# 查看每個部門員工總數,部門平均薪資大於1000,並且每個員工的薪資>900
select dept_id,count(*),avg(salary)
from s_emp
where salary >900
group by dept_id
having avg(salary)>1000

    排序order by:升序ASC;逆序DESC

# 查看員工的員工ID,名字,月薪,部門ID,部門ID進行升序排序,相同部門的員工在一起按照薪資從高到低排序

select id,last_name,dept_id,salary
from s_emp
order by dept_id asc,salary desc;

# 如果進行排序的時候,需要對欄位先進行轉碼,後排序
select id,last_name,dept_id,salary
from s_emp
order by conver(dept_id asc,salary using gbk2312) desc;

 

    限制記錄數目:limit N,M  跳過N條記錄,查詢M條記錄

# 跳過3條記錄,查詢5條記錄,這一般用於分頁比較合理
# 擦昏地當前頁數和記錄數
select id,last_name,dept_id,salary
from s_emp
order by dept_id asc,salary desc
limit 3, 5


# 當數據表的記錄上萬條,比如超過1萬條記錄
# 建議使用子查詢進行分頁,這樣效率高點,因為子查詢是在索引文件(所以你文件比較小,數據文件比較大,處理上就比較慢)上執行的
select id, last_name, dept_id, salary
from s_emp
where id >=(
    select id
    from s_emp
    order by id
    limit 10000, 1
)
limit 10;

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 直接上碼供參考 關鍵點說明: 1.清理windows訪問記錄,包含在任務欄上右鍵出現最近訪問的程式的記錄%UserProfile%\AppData\Roaming\Microsoft\Windows\Recent\AutomaticDestinations%UserProfile%\AppData\ ...
  • 借鑒:https://www.cnblogs.com/shijingjing07/p/9301590.html ...
  • u8 key_return = NO_KEY; u8 key_tmp = NO_KEY; u8 key_lock = 0;//按鍵自鎖標誌,自己加的 if (key_val == NO_KEY || key_val != key_ctl.key_val) //按鍵沒有按下或者本次按鍵與上一次按鍵不相 ...
  • 條件判斷: [ condition ],condition前後都有空格 常用的判斷條件: 1)兩個整數的比較 = 字元串比較 -lt 小於 -le 小於等於 -eq 等於 -gt 大於 -ge 大於等於 -ne 不等於 2)按照文件許可權進行判斷 -r有讀的許可權 -w有寫的許可權 -x有執行的許可權 3) ...
  • 一、數據挖掘 中文分詞 • 一段文字不僅僅在於字面上是什麼,還在於怎麼切分和理解。• 例如: – 阿三炒飯店: – 阿三 / 炒飯 / 店 阿三 / 炒 / 飯店• 和英文不同,中文詞之間沒有空格,所以實現中文搜索引擎,比英文多了一項分詞的任務。• 如果沒有中文分詞會出現: – 搜索“達內”,會出現 ...
  • 公司一SQL Server鏡像發生了故障轉移(主備切換),檢查SQL Server鏡像發生主備切換的原因,在錯誤日誌中發現下麵錯誤: Date 2019/8/31 14:09:17 Log SQL Server (Archive #4 - 2019/9/1 0:00:00) Source spid3... ...
  • redis是key-value的數據,所以每個數據都是一個鍵值對。 數據操作的全部命令,可以查看中文網站。 鍵的類型是字元串 值的類型分為五種: 字元串string 哈希hash 列表list 集合set 有序集合zset 字元串string 哈希hash 列表list 集合set 有序集合zset ...
  • 1.獨立模式(standalone|local) nothing! 本地文件系統。 不需要啟用單獨進程。 2.pesudo(偽分佈模式) 等同於完全分散式,只有一個節點。 SSH: //(Socket), //public + private /server : sshd ps -Af | grep ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...