use mydb;
select * from EMP;
select * from DEPT;
select DISTINCT JOB from EMP; ?-- distinct ? 去除重復項
select MGR from EMP;
select MGR as 主管編號 from EMP; ?-- 輔助查詢,每列信息 ? 起別名 as
select EMPNO as 員工編號,JOB as 職位,DEPNO as 部門編號 from EMP;
select EMPNO 員工編號,JOB ?職位,DEPNO 部門編號 from EMP; ?-- 也可以省略as 用空格替換as
select ename,'2019-08-28' as today from EMP; ?-- 構建一個新的列
-- 條件查詢 ?where
select * from EMP;
-- 關系 ?> ?< ? >= ?<= ? !=(<>) ? = ?between...and
select * from EMP where DEPNO=20;
select * from EMP where DEPNO<>20;
-- 查詢薪資大于1000的員工信息
select * from EMP where sal>2000?
-- or ?and ? not
select * from EMP where sal>3000 and sal<=5000
update EMP set sal=3500 where ?EMPNO=7566
select * from EMP where sal between 3000 and 5000 ? -- sal>=3000 and sal<=5000
-- 查詢薪資大于等于3500或者職位是MANAGER
select * from EMP where sal>=3500 or job='MANAGER'
-- 查詢部門是30并且薪資在2000~3500之間的員工
select * from EMP where DEPNO=30 and sal between 2000 and 3500
-- 查詢30號部門的所有職位有哪些
select distinct job from EMP where depno =30
-- 查詢入職時間是1981-09-30之后的所有員工
select *from EMP where HIREDATE>'1981-09-30'
-- 查詢沒有津貼并且基本工資超出3000元的員工姓名和職位
select ename,job from EMP where comm is NULL and sal>3000
-- 查詢7698管理的所有員工,,,,
select * from EMP where MGR=7698
-- in , not in ,
select * from EMP where ename not in ('FORD','KING','WARD');
-- is null , ? is not null ?
-- 模糊查詢 ? 字符 ?like ? % 任意長度的字符 ?_ 任意一個字符
select * from ?EMP where ename like 'J%' or ename like 'A%'
?
select * from EMP where ename like '王_'
-- 查詢名字中出現'明'同學
select * from EMP where ename like '%明'
-- insert into EMP values(7981,'王小明','CLERK',7698,'1982-02-08',1890,100,30)
-- 排序 order by 字段名 [desc降序 | asc默認升序]
-- 查詢部門編號是30號部門員工信息并按照薪資sal降序
select * from EMP where DEPNO=30 order by sal desc
-- 查詢入職時間是1982之前所有員工,并按照入職日期排序
select * from EMP where hiredate < '1982-01-01' order by hiredate
?
-- 限制 limit offset n
-- 100 ?1-10 ?11-20 ?21-30 ...
-- page ---> 1 ?....&page=3
-- count 每頁5條 ?---》offset = count * (page-1)
select * from EMP;
select * from EMP limit 0,10;
select * from EMP limit 10,10;
select * from EMP limit 20,10;
select * from EMP limit 30,10;
-- 查詢薪資超出3000并且是30號部門的員工 按照入職日期降序
select * from EMP where SAL>3000 and DEPNO=30 ORDER BY HIREDATE DESC;?
-- 查詢入職日期在1981年以后的所有員工,每12條一頁,獲取第2頁的數據
select * from EMP where HIREDATE>'1981-01-01' LIMIT 13,12
-- 查詢職位是ANALYST或者CLERK的所有數據,如果數據比較多則分頁,每5條一頁數據
select * from EMP where job='analyst' or job='clerk' LIMIT 5
-- 查詢沒有津貼的員工并按照sal升序排列
select * from EMP WHERE comm is null ?ORDER BY sal?
-- 查詢姓王或者姓張的名字中有‘紅’字的同學
select * from EMP where ename like '%紅'or ename like '張%'or ename like ('王%');
?
-- 聚合函數: sum ? min ? max ? avg ? count 計數
-- 查詢員工人數
select count(EMPNO) from emp;
select count(empno) from emp where depno=30
select count(empno) as 人數 from emp where ename like '張%'
-- 查詢入職日期是8月入職
select count(empno) 人數 from emp where MONTH(hiredate)=8
select MONTH(hiredate) from emp;
-- ?查詢薪資總和 ?sum(字段)?
select sum(sal) ?from emp;
select sum(sal) 總和 from emp where depno in (10,40);?
-- ?最高工資
select max(sal) from emp;
-- ? 最低工資
select min(sal) from emp;
-- ?平均工資
select avg(sal) from emp;
-- 分組查詢: select 字段,聚合函數 ?from ?表 [where 條件] group by 字段 ?having 條件
-- 先從表中選出第一個字節 ?再讓后邊的通過group by排序? ? 生成第二個字節? ?having 可以限制第二個字節
-- 統計班級男女生的人數
-- 統計各個部門的員工人數
? select depno 部門號,count(*) 人數 from emp group by depno;
?? ?-- select job,depno,count(*) 人數 from emp group by depno;
-- 統計各個職位的人數
select job 職位,count(*) 人數 from emp group by job;
-- 求各個部門的每個職位的人數
select depno 部門號,job 職位, count(*) 人數 from emp group by depno,job;
-- 統計各個部門的最高薪資
select depno 部門號,max(sal) 最高薪資 from emp group by depno;
-- 查詢入職日期在2000年之前的各個部門的人數
select depno,count(*) from emp where hiredate<'1982-01-01' group by depno
-- 查詢薪資超出5000元的各個部門的人數
select depno,count(*) from emp where sal>5000 group by depno;
-- 查詢部門是20號,各個職位的平均薪資
select job,avg(sal) from emp where depno=20 group by job;
-- 查詢姓秦的員工在各個職位中的最低工資
select job,min(sal) from emp where ename like '秦%' group by job;
-- 查詢管理者(職位是manager的)在各個部門的人數
select depno,count(*) from emp where job ='MANAGER' group by depno;
-- 查詢管理者(職位是manager的)在各個部門的人數 按照人數排序
select depno,count(*) from emp where job ='MANAGER' group by depno order by count(*)
-- 查詢管理者(職位是manager的)人數在各個部門的超出15人
select depno,count(*) from emp where job ='MANAGER' group by depno ?having count(*)>=13
-- ?where 先篩選后分組
-- ?having 先分組后篩選 ? having永遠跟group by結合使用的
-- 子查詢: 嵌套查詢
-- 查詢10號部門比平均薪資低員工信息
-- 10號部門
-- 平均薪資
select * from emp where depno =10 and sal<(select avg(sal) from emp);?
-- 查詢工作所在地在紐約的所有員工信息
-- select deptno from dept where loc='NEW YORK'
select * from emp where depno in (select deptno from dept where loc='NEW YORK')
-- 查詢職位是salesman的工作所在地和部門名稱
-- select distinct depno from emp where job = 'SALESMAN'
select dname,loc from dept where deptno in (select distinct depno from emp where job = 'SALESMAN')
-- 查詢沒有拿到津貼的員工所在的部門名稱
select dname from dept where deptno in (select ?distinct ?depno from emp where comm is null or comm=0)
-- 查詢職位是CLERK并且工作所在地在BOSTON和DALLAS的員工信息
select * from emp where job ='CLERK' and depno in (select deptno from dept where loc in ('BOSTON','DALLAS'))
-- 查詢比銷售部門SALES的最低工資低的其他部門員工信息
select * from emp where depno<>(select deptno from dept where dname='SALES') and sal < (select min(sal) from emp where depno =(select deptno from dept where dname='SALES'))
-- 查詢比7900員工入職晚的員工
select * from emp where hiredate> (select hiredate from emp where empno=7900)
-- 顯示工資比’ALLEN’高的所有員工的姓名和工作
select ename,job from emp where sal>(select sal from emp where ename='allen')
-- 顯示與scott從事相同工作的員工的信息
select * from emp where job=(select job from emp where name='scott')
-- 連接查詢
-- 查詢工作所在地在紐約的所有員工信息,顯示員工姓名,職位,工作所在地
select * from emp,dept; ? -- emp 914 ? dept:5 ?---> 4570
-- 等值連接查詢 ?
select * from emp,dept where emp.depno = dept.deptno;
-- 查詢工作所在地在紐約的所有員工信息,顯示員工姓名,職位,工作所在地
select ename,job,loc from emp,dept where loc='NEW YORK' and emp.depno = dept.deptno;
-- 查詢工作是CLERK 并且所在地在NEW YORK的員工姓名
select ename from emp e,dept d where e.job='CLERK' and d.loc='NEW YORK' and e.depno = d.deptno;
-- 內連接 表1 inner join 表2 ?on ?連接條件
select e.ename from emp e inner join dept d ?on e.depno = d.deptno where ?e.job='CLERK' and d.loc='NEW YORK'
-- 查詢薪資低于4500的員工信息顯示姓名,工作,部門名稱
select e.ename,e.job,d.dname from emp e join dept d on e.depno = d.deptno where e.sal<4500 ? -- 內連接
-- 顯示工資比’ALLEN’高的所有員工的姓名和工作
select e.ename,e.job ?from emp e where sal > (select sal from emp where ename='ALLEN')
-- 顯示與scott從事相同工作的員工的信息
select * from emp where job = (select job from emp where ename='SCOTT')
-- 外連接: ?使用外連接篩選一些不能構成等值記錄
-- 左外連接 ? left join ...on ? 左側表的記錄全部出現,右側表中的記錄是能匹配的全部顯示,不能匹配則全部顯示null
-- 右外連接 ? right ?join ...on
select * from emp left join dept on emp.depno=dept.deptno where emp.empno<7939 and dept.deptno is null;
-- emp ?right join ?dept ? ?dept 主表 ? emp 從表 ? ?主表信息要全部顯示
select * from emp right join dept on emp.depno=dept.deptno where emp.empno<7939 and dept.deptno is null;
-- emp left join ?dept ? ?emp 主表 ? dept 從表
select * from emp left join dept on emp.depno=dept.deptno?
select * from dept right join emp on emp.depno=dept.deptno
?
-- 自連接
-- 查詢所有員工和員工職位和管理者名字
select ename,job,mgr from emp;
select * ?from emp a join emp b on a.empno = b.mgr
select a.ename 主管姓名, b.ename 員工姓名 from emp a join emp b on a.empno = b.mgr
-- 系統函數:
select length('張三')
select CHAR_LENGTH('張三')
select upper(dname) from dept
select lpad('hello',8,'A')
select repeat('good',3)
create table aa(id int primary key,name varchar(16),birthday date)
insert into aa(id,name,birthday) values(1,'小明',curdate())
select curdate()
select curtime()
select * from aa;
select week(birthday) from aa;
update aa set birthday = DATE_ADD(birthday,INTERVAL -1 YEAR)?
select DATEDIFF(birthday,curdate()) from aa;
-- select TIMEDIFF(expr1,expr2)
表格插入的部分數據:
create table EMP create database mydb charset=utf8; |
筆記內容:mysql:
DDL:create drop alter
create database 數據庫名 [charset=utf8];
create table 表(字段field 數據類型 ,….字段 decimal(7,2),...)
數據類型: int bigint smallint tinyint
float double decimal
char(9) varchar(9)
A__________
AB
date datetime time year約束:
主鍵 primary key pk
外鍵 foreign key fk
唯一 unique
默認 default
非空 not nullalter table 表 add 字段 數據類型 約束alter table 表 drop 字段 alter table 表 modify 字段 數據類型 約束alter table 表 change old字段 new 字段 數據類型 約束刪除:
drop database 數據庫
drop database 表名DML: insert update deleteinsert into student(字段名,….) values(值,。。。)insert into student values()insert into student(字段名,….) values(…..),(…...),(……)update 表名 set 字段 salary = salary+1000 [where 條件]delete from 表名 [where 條件]DQL:query 數據查詢語言 select基礎查詢:
select 字段名,字段名,.. from 表名 [where 條件]select * from 表名; * 所有字段select 字段 from 表名[where 條件][group by ][having][order by ][limit] - 字符串函數| 函數 | 功能 || ------------------------ | ------------------------------------------------------------ || char_length(*str*) | 獲取字符串的字符個數 || length(str) | 獲取字符串的字節數 || concat(s1, s2, ... , sn) | 連接s1, s2, ..., sn 為一個字符串 || lower(str) | 將字符串str中所有的字符轉換為小寫 || upper(str) | 將字符串str中所有的字符轉換為大寫 || left(str, x) | 返回字符串str最左邊的x個字符 || right(str, y) | 返回字符串str最右邊的y個字符 || lpad(str, n, pad) | 用字符串pad對str最左邊進行填充, 直到長度為n個字符長度 || rpad(str, n, pad) | 用字符串pad對str最右邊進行填充, 直到長度為n個字符長度 || ltrim(str) | 去掉str中最左邊的空格 || rtrim(str) | 去掉str中最右邊的空格 || trim(str) | 去掉字符串str兩邊的空格 || repeat(str, x) | 返回str中重復出現x次的結果 || replace(str, a, b) | 將字符串str中的a更換為b || insert(str, x, y, instr) | 將字符串str從第x位置開始, y個字符長度的子字符串替換為字符串instr || strcmp(s1, s2) | 比較字符串s1, s2 || substring(str, x, y) | 返回字符串str x位置開始y個字符長度的字符串 |- 日期函數| 函數名 | 功能 || --------------------- | --------------------------------- || curdate() | 得到當前日期 || curtime() | 得到當前時間 || now() | 得到當前日期和時間 || year(date) | 得到date的年份 || month(date) | 得到date的月份 || day(date) | 得到date的天 || hour(time) | 得到time的小時 || minute(time) | 得到time 的分鐘 || second(time) | 得到time的秒 || week(date) | 得到date是一年中的第幾周 || date_format(date,fmt) | 按格式化串fmt返回date的日期字符串 |DATE_ADD(date, INTERVAL number unit)date_sub()
datediff(date1,date2)select DATE_FORMAT(now(),'%Y- %m-%d %H:%i:%s'); - 數學函數| 函數名 | 功能 || ---------- | --------------------------- || abs(x) | 求x的絕對值 || ceil(x) | 向上取整 || floor(x) | 向下取整 || round(x,d) | 四舍五入,d為保留小數的位數 || pow(x,y) | x的y次冪 || rand() | 0~1之間的隨機小數 || mod(x,y) | 等同于x % y,求x對y的模 |
?