簡單的SQL語句的快速復習

語法的執行順序
?

select 4 字段列表
?

from 1 表名列表
?

where 2 條件列表
?

group by 3 分組前過濾
?

having 分組后過濾
?

order by 5 排序字段列表
?

limit 6 分頁參數


聚合函數
?

count 統計數量
?

max 最大值
?

min 最小值
?

avg 平均
?

sum 總和


分組查詢使用例子

1.性別分組,統計數量
?

select gender ,count(*)from emp group by gender
?


?

2.性別分組 統計平均年齡
?

select age ,avg(age) from emp group by gender
?


?

3.查詢年齡小于45的員工,并且按照工作地址分組,獲取員工數量>=3的工作地址
?

首先
?

按工作地址分組然后獲取年齡小于45的員工的地址信息的總數
?

select workaddress,count(*)from emp where age<45 group by workaddress

?

再分組完后進行過濾
?

having count(*)>=3


排序查詢使用例子

語法:
?

select 字段列表 from 表名 order by 字段1 排序方式1,字段2,排序方式2;
?


?

排序方式:
?

1.ASC:升序(默認)
?

2.DESC:降序
?


?

例子
?

1.根據年齡進行排序,2.年齡相同,再入職日期降序排序
?

select age from emp order by ageasc,entrydate desc
?

多字段排序:第一個字段相同時再進行第二個字段排序


分頁查詢
?

語法
?

select 字段列表 from 表名 limit 起始索引,查詢記錄數;
?

select * from emp limit 0 10


函數

字符串函數
?

concat(s1,s2,s3)字符串的拼接

?

lower(str) 小寫


?

upper(str)大寫
?


?

lpad(str,n,pad)
?

左填充,用字符串pad對左邊進行填充,達到n個字符串長度
?


?

rpad(str,n,pad)
?

右填充

?

trim(str)
?

去掉字符串頭部和尾部的空格

?

substring(str,start,len)
?

返回字符串str從str位置起的len個長度的字符串
?


?


?

int類型不能補0,因為是整形但可以補1
?


數值函數
?

ceil(x) 向上取整
?

floor(x) 向下取整
?


?

mod(X,Y) 返回x/y的模
?


?

rand()返回0-1內的隨機數
?


?

round(X,Y)四舍五入,保留y位小數
?


日期函數
?

curdate() 日期
?

curtime() 時間
?

now() 現在
?


year(date) 獲取指定date的年份
?

month(date) 獲取指定date的月份
?


?

day(date) 日期
?


?

date-add(date,interval exprtype)
?

返回這個日期加上一個時間間隔后的時間值
?


?

datediff(date1,date2)
?

返回起始時間date1和結束時間date2之間的天數


流程函數
?

if(value,t,f)
?

true返回t
?

false返回f
?


?

ifnull(value1,value2)不空返回value1,空的話返回value2
?


?

case when then
?


?

case when [val]then [res1] else [defaulse] End
?


?

val為true則返回res1
?

否則返回default默認值
?


?

case [expr] when [val] then [res1] else [default] End
?

end是結束
?

當expr的值等于val時返回res1否則返回default
?


?

使用例子
?

select name,(case workaddress when'北京' then'一線',when‘上海’,then‘一線’ end)as‘工作地址’
?


增刪改查

添加數據
?

1.給指定字段添加數據 insert values
?

insert into 表名(字段1 , 字段2) values(值1,值2)
?


?


?

2.給全部字段添加數據(不寫出具體字段名)
?

insert into 表名 values(值1,值2)
?


?

3.批量添加數據
?

給特定字段
?

insert into 表名(字段1,字段2)values(值1,值2)(值1,值2);
?

給全部字段
?

insert into 表名 values (值1,值2), (值1,值2), (值1,值2);


更新與刪除


?

修改數據:update set
?

update 表名 set 字段名1=值1,字段名2=值2......[where 條件]
?

不寫條件where的話就是所有都執行
?


?

刪除數據 delete
?

delete from 表名 [where 條件]
?


聯合查詢(union)

union查詢,就是把多次的查詢結果合并起來形成一個新的查詢結果

?

select 字段列表 from 表a
?

union[all]
?

select 字段列表 from 表b
?


?

分別查詢薪資>5000,年齡>50的員工
?

select *from emp where salary>5000
?


?

union all
?


?

select *from emp where age>50
?

但是結果會有重復的,為了去重
?

可以把all去
?


報錯情況

select *from emp where salary>5000
?

union all
?

select name from emp where age>50
?

這個會發現報錯
?

因為對于聯合查詢來說。字段表的列數字段類型必須保持一致
?


子查詢

子查詢
?

又稱為 嵌套查詢
?

標量子查詢
?

查詢銷售部的所有員工信息
?

1. select id from emp where name='銷售部';
?

第一部查詢出銷售部id等于4
?

2.select *from emp where dept_id=4;
?

要兩條指令,但我們想用一條搞定
?

select *from emp where dept_id=(select id from emp where name='銷售部');
?



?

列子查詢
?

常用操作符

in,not in,any,some,all
?

1
?

select id from dept where name='銷售部'or name='市場部';
?

查出的id是1和2
?

然后
?

select* from emp where dept_id in (1,2)
?

或者
?

select* from emp where dept_id in (select id from dept where name='銷售部'or name='市場部');
?

2
?

查詢比財務部的所有人工資都高的員工的 信息
?

a 查詢所有財務部人員的工資
?

select id from dept where name='財務部';
?

select salary from emp where dept_id=3
?


?

b查詢比財務部所有人工資都高的員工信息
?

select *from emp where salary> all( select salary from emp where dept_id=3)
?


?

3
?

查詢比研發部其中任意一人工資都高的員工信息
?

因為是任意一人所以 沒有all
?


行子查詢
?

查詢與‘張無忌’薪資以及領導都相同的員工的信息


?

a.查詢張無忌的工資及其領導
?

select salary,managerid from emp where name='張無忌'


?

b. 查詢員工
?

select *from emp where salary=12500 and mangerid =1;
?

或者
?

select *from emp where (salary,managerid)=(12500,1);
?

再或者
?

select *from emp where(salary,mangerid)=(select salary,managerid from emp where name='張無忌')
?



?

表子查詢
?

常用操作符 in
?

1.查詢與‘路’和‘白’薪資以及職位相同的員工
?

select job,salary from emp where name='路'or name=‘白’
?

select * from emp where (job,salary) in(select job,salary from emp where name='路'or name=‘白’)
?


?

2.查詢入職日期是“2006-01-01”之后的員工信息,及其部門信息
?

select * from emp where entrydate>"2006-01-01"
?

把上面那個作為臨時表
?

select e.*,d.* from(select * from emp where entrydate>"2006-01-01") e left join dept d on e.dept_id=d.id
?


多表聯查

1.查詢員工的姓名,年齡,職位,部門信息(隱式內連接)
?

表:emp dept
?

連接條件:emp.dept_id=dept.id
?

記得消除笛卡爾積
?

select e.name,e.age,e.job,d.name from emp e, dept d where e.dept_id=d.id;
?


?

2.查詢年齡小于30歲的員工的姓名,年齡,職位,部門信息(顯示內連接)
?

select e.name,e.age,e.job,d.name from emp e inner join dept d on e.dept_id=d.id where e.age<30
?


?

3.查詢擁有員工的部門id和部門名稱
?

求取員工表和部門表之間的交集用內連接
?

select d.id,d.name from emp e,dept d where e.dept_id=d.id
?

此時會有多個重復的部門,因為他是按照員工數量來的
?

去重復用 distinct
?

select distinct d.id,d.name from emp e,dept d where e.dept_id=d.id
?


?

4.查詢所有年齡大于40的員工,及其歸屬部門的名稱;如果員工沒有分配部門也要顯示出來
?

要用外連接
?

select e.*,d.name from emp e left join dept d on e.dept_id=d.id where e.age>40
?


?

5.查詢所有員工的工資等級
?

表:emp salarygrade
?

連接條件:emp.salary >=salagrade.losal and emp.salary<=salagrade.hisal
?


?

select e.*,s.grade emp e,salagrade s where e.salary>=s.losal and e.salary <=s.hisal
?


?

第二種寫法:
?

select e.*,s.grade emp e,salagrade s where e.salary between s.losal and s.hisal
?


?

6.查詢 研發部 所有員工的信息以及工資等級
?

涉及到的表:emp dept salgrade
?

連接條件:
?

emp.salary between s.losal and s.hisal
?


?

emp.dept_id=dept.id
?


?

查詢條件 dept.name='研發部'
?


?

select e.*,s.grade from emp e ,dept d,salgrade s where e.dept_id=d.id and ( emp.salary between s.losal and s.hisal)and d.name='研發部'
?


?

7.查詢研發部員工的平均工資
?

表 emp dept
?

select avg(e.salary) from emp e, dept d where e.dept_id=d.id and e.name='研發部'
?



?

8.查詢工資比‘滅絕’高的員工信息
?

select * from emp where salary>(select salary from emp where name='滅絕')
?

查詢滅絕的薪資
?

select salary from emp where e.name='滅絕'
?


?

9.查詢比平均薪資高的員工信息
?

select avg(salary) from emp
?

select * from emp where salary>(select avg(salary) from emp)
?


?

10.查詢低于 本部門 平均薪資的員工
?

a.查詢指定部門的平均薪資
?

select avg(e.salary) from emp e where e.dept_id=1
?

select avg(e.salary) from emp e where e.dept_id=2
?


?

b.
?

select *from emp e2 where salary<(select avg(e.salary) from emp e where e.dept_id=e2.dept_id)
?

保證平均下來的薪資是同一個部門的
?


?

11.查詢所有的部門信息,并統計部門的員工人數
?

a.查詢信息
?

select id,name from dept
?

b.查詢指定部門的人數
?

select count(*) from emp where dept_id=1
?


?

最終
?

select d.id ,d.name (select count(*) from emp e where e.dept_id=id)'人數' from dept d;
?


?


?

12.查詢所有學生的選課情況,展示出學生的名稱,學號,課程名稱
?


?

表:student ,course,student_course
?

連接條件:student.id=student_course.studentid,course.id=student_course.courseid
?


?

select s.name ,s.no,c.name from student s,student_course sc,course c where s.id=sc.studentid and sc.courseid=c.id
?

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

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

相關文章

《程序人生》工作2年感悟

一些雜七雜八的感悟&#xff1a; 1.把事做好比什么都重要&#xff0c; 先樹立量良好的形象&#xff0c;再橫向發展。 2.職場就是人情世故&#xff0c;但也不要被人情世故綁架。 3.要常懷感恩的心&#xff0c;要記住幫助過你的人&#xff0c;愿意和你分享的人&#xff0c;有能力…

17.2 圖形繪制8

版權聲明&#xff1a;本文為博主原創文章&#xff0c;轉載請在顯著位置標明本文出處以及作者網名&#xff0c;未經作者允許不得用于商業目的。 17.2.10 重繪 先看以下例子&#xff1a; 【例 17.28】【項目&#xff1a;code17-028】繪制填充矩形。 private void button1_Clic…

自定義數據集 使用pytorch框架實現邏輯回歸并保存模型,然后保存模型后再加載模型進行預測,對預測結果計算精確度和召回率及F1分數

import numpy as np import torch import torch.nn as nn import torch.optim as optim from sklearn.metrics import precision_score, recall_score, f1_score# 數據準備 class1_points np.array([[1.9, 1.2],[1.5, 2.1],[1.9, 0.5],[1.5, 0.9],[0.9, 1.2],[1.1, 1.7],[1.4,…

neo4j入門

文章目錄 neo4j版本說明部署安裝Mac部署docker部署 neo4j web工具使用數據結構圖數據庫VS關系數據庫 neo4j neo4j官網Neo4j是用ava實現的開源NoSQL圖數據庫。Neo4作為圖數據庫中的代表產品&#xff0c;已經在眾多的行業項目中進行了應用&#xff0c;如&#xff1a;網絡管理&am…

腳本運行禁止:npm 無法加載文件,因為在此系統上禁止運行腳本

問題與處理策略 1、問題描述 npm install -D tailwindcss執行上述指令&#xff0c;報如下錯誤 npm : 無法加載文件 D:\nodejs\npm.ps1&#xff0c;因為在此系統上禁止運行腳本。 有關詳細信息&#xff0c;請參閱 https:/go.microsoft.com/fwlink/?LinkID135170 中的 about_…

Java基礎——分層解耦——IOC和DI入門

目錄 三層架構 Controller Service Dao ?編輯 調用過程 面向接口編程 分層解耦 耦合 內聚 軟件設計原則 控制反轉 依賴注入 Bean對象 如何將類產生的對象交給IOC容器管理&#xff1f; 容器怎樣才能提供依賴的bean對象呢&#xff1f; 三層架構 Controller 控制…

智慧園區系統集成解決方案引領未來城市管理的智能化轉型

內容概要 在現代城市管理的背景下&#xff0c;“智慧園區系統集成解決方案”正扮演著越來越重要的角色。這種解決方案不僅僅是技術上的創新&#xff0c;更是一種全新的管理理念&#xff0c;它旨在通過高效的數據整合與分析&#xff0c;優化資源配置&#xff0c;提升運營效率。…

99.24 金融難點通俗解釋:MLF(中期借貸便利)vs LPR(貸款市場報價利率)

目錄 0. 承前1. 什么是MLF&#xff1f;1.1 專業解釋1.2 通俗解釋1.3 MLF的三個關鍵點&#xff1a; 2. 什么是LPR&#xff1f;2.1 專業解釋2.2 通俗解釋2.3 LPR的三個關鍵點&#xff1a; 3. MLF和LPR的關系4. 傳導機制4.1 第一步&#xff1a;央行調整MLF4.2 第二步&#xff1a;銀…

【VM】VirtualBox安裝CentOS8虛擬機

閱讀本文前&#xff0c;請先根據 VirtualBox軟件安裝教程 安裝VirtualBox虛擬機軟件。 1. 下載centos8系統iso鏡像 可以去兩個地方下載&#xff0c;推薦跟隨本文的操作用阿里云的鏡像 centos官網&#xff1a;https://www.centos.org/download/阿里云鏡像&#xff1a;http://…

Elasticsearch中的度量聚合:深度解析與實戰應用

在大數據和實時分析日益重要的今天&#xff0c;Elasticsearch以其強大的搜索和聚合能力&#xff0c;成為了眾多企業和開發者進行數據分析和處理的首選工具。本文將深入探討Elasticsearch中的度量聚合&#xff08;Metric Aggregations&#xff09;&#xff0c;展示其如何在數據分…

C_C++輸入輸出(下)

C_C輸入輸出&#xff08;下&#xff09; 用兩次循環的問題&#xff1a; 1.一次循環決定打印幾行&#xff0c;一次循環決定打印幾項 cin是>> cout是<< 字典序是根據字符在字母表中的順序來比較和排列字符串的&#xff08;字典序的大小就是字符串的大小&#xff09;…

電腦要使用cuda需要進行什么配置

在電腦上使用CUDA&#xff08;NVIDIA的并行計算平臺和API&#xff09;&#xff0c;需要進行以下配置和準備&#xff1a; 1. 檢查NVIDIA顯卡支持 確保你的電腦擁有支持CUDA的NVIDIA顯卡。 可以在NVIDIA官方CUDA支持顯卡列表中查看顯卡型號是否支持CUDA。 2. 安裝NVIDIA顯卡驅動…

深入解析:一個簡單的浮動布局 HTML 示例

深入解析&#xff1a;一個簡單的浮動布局 HTML 示例 示例代碼解析代碼結構分析1. HTML 結構2. CSS 樣式 核心功能解析1. 浮動布局&#xff08;Float&#xff09;2. 清除浮動&#xff08;Clear&#xff09;3. 其他樣式 效果展示代碼優化與擴展總結 在網頁設計中&#xff0c;浮動…

家居EDI:Hom Furniture EDI需求分析

HOM Furniture 是一家成立于1977年的美國家具零售商&#xff0c;總部位于明尼蘇達州。公司致力于提供高品質、時尚的家具和家居用品&#xff0c;滿足各種家庭和辦公需求。HOM Furniture 以廣泛的產品線和優質的客戶服務在市場上贏得了良好的口碑。公司經營的產品包括臥室、客廳…

【VUE案例練習】前端vue2+element-ui,后端nodo+express實現‘‘文件上傳/刪除‘‘功能

近期在做跟畢業設計相關的數據后臺管理系統&#xff0c;其中的列表項展示有圖片展示&#xff0c;添加/編輯功能有文件上傳。 “文件上傳/刪除”也是我們平時開發會遇到的一個功能&#xff0c;這里分享個人的實現過程&#xff0c;與大家交流談論~ 一、準備工作 本次案例使用的…

C++中的析構器(Destructor)(也稱為析構函數)

在C中&#xff0c;析構器&#xff08;Destructor&#xff09;也稱為析構函數&#xff0c;它是一種特殊的成員函數&#xff0c;用于在對象銷毀時進行資源清理工作。以下是關于C析構器的詳細介紹&#xff1a; 析構函數的特點 名稱與類名相同&#xff0c;但前面有一個波浪號 ~&a…

VLN視覺語言導航基礎

0 概述 視覺語言導航模型旨在構建導航決策模型 π π π&#xff0c;在 t t t時刻&#xff0c;模型能夠根據指令 W W W、歷史軌跡 τ { V 1 , V 2 , . . . , V t ? 1 } \tau\{V_1,V_2,...,V_{t-1}\} τ{V1?,V2?,...,Vt?1?}和當前觀察 V t { P t , R t , N ( V t ) } V_…

AI協助探索AI新構型的自動化創新概念

訓練AI自生成輸出模塊化代碼&#xff0c;生成元代碼級別的AI功能單元代碼&#xff0c;然后再由AI組織為另一個AI&#xff0c;實現AI開發AI的能力&#xff1b;用AI協助探索迭代新構型AI將會出現&#xff0c;并成為一種新的技術路線潮流。 有限結點&#xff0c;無限的連接形式&a…

Flux的三步煉丹爐——fluxgym(三):矩陣測試

前面兩篇文章給大家介紹了如何準備素材和怎么煉丹&#xff0c;現在我們拿到訓練完成后的多個Lora怎么才能確定哪個才是我們需要的、效果最好的呢&#xff1f;答案就是使用xyz圖表測試&#xff0c;也稱為矩陣測試&#xff0c;通過控制控制變量的方法對Lora模型批量生圖&#xff…

利用Muduo庫實現簡單且健壯的Echo服務器

一、muduo網絡庫主要提供了兩個類&#xff1a; TcpServer&#xff1a;用于編寫服務器程序 TcpClient&#xff1a;用于編寫客戶端程序 二、三個重要的鏈接庫&#xff1a; libmuduo_net、libmuduo_base、libpthread 三、muduo庫底層就是epoll線程池&#xff0c;其好處是…