数据库基础知识——DQL语言(一)
生活随笔
收集整理的這篇文章主要介紹了
数据库基础知识——DQL语言(一)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 1.基礎查詢
- 2.條件查詢
- 3.排序查詢
- 4.常見函數
- 4.1 單行函數
- 4.1.1 字符函數
- 4.1.2 數學函數
- 4.1.3 日期函數
- 4.1.4 流程控制函數
- 4.1.5 其他函數
- 4.2 分組函數/統計函數/聚合函數
- 5.分組查詢
1.基礎查詢
語法: SELECT 要查詢的東西 【FROM 表名】; #查詢employees表中所有字段 select * from employees;#查詢employees表中的first_name字段 select first_name from employees;#查詢employees表中的多個字段 select first_name,salary FROM employees;#查詢常量值 select 100+123; select "zhaoxr"#查詢表達式 select 100*98;#查詢函數 select version();#起別名 select 98*98 as result; select last_name as "姓",first_name as "名" from employees; select last_name "姓",first_name "名" from employees;#去重 select distinct department_id from employees;# +的作用 select 100+99; select "123"+99; select "zhaoxr"+"linux"; select null+16;# concat函數 select concat(last_name," ",first_name) "姓名" from employees; 類似于Java中 :System.out.println(要打印的東西); 特點: ① 通過select查詢完的結果 ,是一個虛擬的表格,不是真實存在 ② 要查詢的東西 可以是常量值、可以是表達式、可以是字段、可以是函數2.條件查詢
條件查詢:根據條件過濾原始表的數據,查詢到想要的數據 語法: select 要查詢的字段|表達式|常量值|函數 from 表 where 條件 ;分類: 一、條件表達式示例:salary>10000條件運算符:> < >= <= = != <>二、邏輯表達式 示例:salary>10000 && salary<20000邏輯運算符:and(&&):兩個條件如果同時成立,結果為true,否則為falseor(||):兩個條件只要有一個成立,結果為true,否則為falsenot(!):如果條件成立,則not后為false,否則為true三、模糊查詢 like between and in is null | is not null通配符: %:任意多個字符,包含0個字符 _:單個字符 \:轉義字符,"\_"代表字符_示例:last_name like 'a%' # 查詢工資大于等于12000的員工ID,姓名和薪水 select employee_id,CONCAT(last_name," ",first_name) as "姓名",salary from employees where salary>=12000;# 查詢部分編號不等于90的員工姓名和部門編號 select CONCAT(last_name," ",first_name) as "姓名",department_id from employees where department_id!=90;#查詢工資在10000到20000之間的員工ID,姓名和工資 select employee_id,concat(last_name," ",first_name) as "姓名",salary from employees where salary>=10000 and salary<=20000;#查詢員工姓中含有a的員工信息 select * from employees where last_name like "%a%";#查詢員工姓第三個字母是l,第五個是e的員工信息 select * from employees where last_name like "__l_e%"; #查詢員工姓的第二個字符是_的員工信息 select * from employees where last_name like "_\_%";#查詢員工ID在100到200之間的員工信息,包含100和200 select * from employees where employee_id between 100 and 200;#查詢工種編號是IT_PROG,PU_CLERK,ST_CLERK的員工信息 select * from employees where job_id in("IT_PROG","PU_CLERK","ST_CLERK");#查詢獎金率為null的員工信息 select * from employees where commission_pct is null;3.排序查詢
語法: select要查詢的東西 from表 where 條件order by 排序的字段|表達式|函數|別名 【asc|desc】 #查詢員工信息,工資由低到高排列 select * from employees order by salary asc;#查詢員工信息,部門編號大于等于90,并且按照入職先后排列 select * from employees where department_id>=90 order by hiredate asc;#查詢員工信息和年薪,并按照年薪從低到高排列 select *,salary*12*(1+ifnull(commission_pct,0)) as "年薪" from employees order by salary*12*(1+ifnull(commission_pct,0)) asc;#查詢員工的姓名和工資,按照姓名的長度從小到大排列 select CONCAT(last_name," ",first_name) as "姓名",length(concat(last_name," ",first_name)) as "姓名長度",salary from employees order by length(concat(last_name," ",first_name)) asc;#查詢員工信息,先按工資從低到高排名,再按員工編號從低到高排序 select * from employees order by salary asc,employee_id asc;4.常見函數
4.1 單行函數
4.1.1 字符函數
concat拼接substr截取子串upper轉換成大寫lower轉換成小寫trim去前后指定的空格和字符ltrim去左邊空格rtrim去右邊空格replace替換lpad左填充rpad右填充instr返回子串第一次出現的索引length 獲取字節個數 #length函數|查詢員工的姓,以及姓的長度 select last_name,length(last_name) from employees;#concat函數|查詢員工的姓名 select concat(last_name," ",first_name) as "姓名" from employees;#upper函數和lower函數|查詢員工的姓名,姓大寫,名小寫 select concat(upper(last_name)," ",lower(first_name)) as "姓名" from employees;#substr截取函數|注意:截取函數從1開始 #返回"i love china"中的"china" select substr("i love china",8);#返回"我愛你中國"中"中國" select substr("我愛你中國",4);#返回"i love china"中的"love" select substr("i love china",3,4);#instr返回子串第一次出現的索引|返回"i love china"中的'c'在第幾個位置 select instr("i love china","c");#trim去空格|刪除" love "左右的空格 select trim(" love ");#trim去除指定的字符|刪除"aaaaaloveaaaa"左右的'a' select trim('a' from "aaaaaaaloveaaaaa");#lpad填充|使用'0'填充字符"我愛你",總字符數10個,填充結果為"0000000我愛你" select lpad("我愛你",10,'0');#replace替換|將"我愛china",替換為"我愛中國" select replace("我愛china","china","中國");4.1.2 數學函數
round 四舍五入rand 隨機數floor向下取整ceil向上取整mod取余truncate截斷 #數學函數 #round四舍五入|2.55四舍五入保留整數部分 select round(2.55);#ceil向上取整,返回>=該數的最小整數|取整2.0001,2.0000 select ceil(2.0001),ceil(2.0000);#floor向下取整,返回<=該數的最大整數|取整-2.0001,2.0000 select floor(-2.0001),floor(2.0000);#truncate截斷,保留小數幾位|1.99999保留兩位小數 select truncate(1.99999,2);#mod取余|10/3的余數 select mod(10,3);4.1.3 日期函數
now當前系統日期+時間curdate當前系統日期curtime當前系統時間str_to_date 將字符轉換成日期date_format將日期轉換成字符 #時間函數 #查詢當前時間 select now();#獲取指定的年月日,獲取當前年份,月份,日 select year(now()) as "年",month(now()) as "月",day(now()) as "日";#獲取員工姓名和入職年份 select concat(last_name," ",first_name) as "姓名",year(hiredate) as "入職年份" from employees;#str_to_date將日期格式的字符轉換為規定的日期格式,規定的日期格式"2021-04-29" select str_to_date("2021年4月29日","%Y年%m月%d日"),str_to_date("04/29/2021","%m/%d/%Y");#date_format將日期轉換為自己想要的日期字符|查詢員工的姓名和入職時間,入職時間顯示為"2021年4月29日"的格式 select concat(last_name," ",first_name) as "姓名",date_format(hiredate,"%Y年%m月%d日") as "日期" from employees;4.1.4 流程控制函數
if 處理雙分支if(判斷條件,真就執行此處,假就執行此處)case 要判斷的字段或者表達式when 常量1 then 要顯示的值1或者語句1when 常量2 then 要顯示的值2或者語句2。。。。。else 要顯示的值n或者語句nendcase when 條件1 then 要顯示的值1或者語句1when 條件2 then 要顯示的值2或者語句2。。。。。else 要顯示的值n或者語句nend #流程控制函數 #if函數|查詢員工的姓,獎金率,如果有獎金,返回有獎金,沒有獎金返回沒獎金 select last_name,commission_pct,if(commission_pct is null,"沒獎金","有獎金") as "獎金" from employees;#case|查詢員工的姓名,工資,部門ID#如果部門=90,salary*1.1;如果部門=100,salary*1.2;如果部門=110,salary*1.3;其它salary不變;并顯示 select concat(last_name," ",first_name),department_id,salary as "原始工資",case department_idwhen 90 then salary*1.1when 100 then salary*1.2when 110 then salary*1.3else salaryend as "當前工資" from employees;#case|查詢員工的姓名,工資#如果工資小于10000,顯示低工資;#如果工資大于等于10000,小于20000,顯示中等工資;#如果工資大于等于20000,顯示高工資 select concat(last_name," ",first_name),salary,case when salary<10000 then "低工資"when salary>=10000 and salary<20000 then "中等工資"when salary>20000 then "高工資"end as "工資區間" from employees;4.1.5 其他函數
version版本database當前庫user當前連接用戶 #其它函數 #version查看版本號 select version();#database查看當前數據庫 select database();#user查看當前用戶 select user();4.2 分組函數/統計函數/聚合函數
sum 求和max 最大值min 最小值avg 平均值count 計數 #分組函數 #簡單使用 select sum(salary) as "總工資",max(salary) as "最高工資",min(salary) as "最低工資",avg(salary) as "平均工資",count(salary) as "工資個數" from employees;#查詢有多少中工資 select count(distinct salary) from employees;#查詢employees表有多少行 select count(*) as "行數",count(1) as "行數" from employees; 特點:1、以上五個分組函數都忽略null值,除了count(*)2、sum和avg一般用于處理數值型max、min、count可以處理任何數據類型3、都可以搭配distinct使用,用于統計去重后的結果4、count的參數可以支持:字段、*、常量值,一般放1建議使用 count(*)5.分組查詢
語法: select 查詢的字段,分組函數 from 表 group by 分組的字段特點: 1、可以按單個字段分組 2、和分組函數一同查詢的字段最好是分組后的字段 3、分組篩選針對的表 位置 關鍵字 分組前篩選: 原始表 group by的前面 where 分組后篩選: 分組后的結果集 group by的后面 having4、可以按多個字段分組,字段之間用逗號隔開 5、可以支持排序 6、having后可以支持別名 #查詢每個部門的平均工資 select avg(salary),department_id from employees group by department_id;#查詢每個工種的最高工資 select max(salary),job_id from employees group by job_id;#查詢工資大于10000的每個工種的最高工資 select max(salary),job_id from employees where salary>10000 group by job_id;#查詢哪個部門員工的個數大于2 select count(*),department_id from employees group by department_id HAVING count(*)>2;#按員工的姓名長度分組,查詢每一組員工的個數,篩選員工個數大于5的有哪些 select count(*),length(last_name) as "len_name" from employees group by length(last_name) having count(*)>5;#查詢每個部門每個工種的平均工資 select avg(salary),department_id,job_id from employees group by department_id,job_id;#查詢每個部門每個工種的平均工資,并按照降序排列 select avg(salary),department_id,job_id from employees group by department_id,job_id order by avg(salary) desc;總結
以上是生活随笔為你收集整理的数据库基础知识——DQL语言(一)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 项目:NMEA2000的数据格式解析
- 下一篇: linux cmake编译源码,linu