系統學習----表記錄操作


記錄操作:增刪改查

插入數據

  1. 插入完整數據(順序插入)
  2. 語法一: INSERT INTO 表名(字段1,字段2,字段3…字段n) VALUES(值1,值2,值3…值n);
    語法二: INSERT INTO 表名 VALUES (值1,值2,值3…值n);
  3. 指定字段插入數據
    語法: INSERT INTO 表名(字段1,字段2,字段3…) VALUES (值1,值2,值3…);
  4. 插入多條記錄
    語法: INSERT INTO 表名 VALUES (值1,值2,值3…值n), (值1,值2,值3…值n), (值1,值2,值3…值n);
  5. 插入查詢結果
    語法: INSERT INTO 表名(字段1,字段2,字段3…字段n) SELECT (字段1,字段2,字段3…字段n) FROM 表2 WHERE …;
insert into stu_info(id,name,age) select stu_id,stu_name,age2 from new_stu_info where stu_id = 11;

更新數據

語法:
UPDATE 表名 SET
字段1=值1,
字段2=值2,
WHERE CONDITION;

實例:

 UPDATE mysql.user SET password=password(‘123’)           
 	where user=’root’ and host=’localhost’;=
update stu_info set name="EE",age=300 where id=5

刪除數據

語法:
DELETE FROM 表名
WHERE CONITION;

實例:

 DELETE FROM mysql.user           
 	WHERE password=’’; 
delete from stu_info where age=18;

查詢數據

單表查詢

  • 語法
    SELECT DISTINCT 字段1,字段2… FROM 表名
    WHERE 條件
    GROUP BY field
    HAVING 篩選
    ORDER BY field
    LIMIT 限制條數

  • 優先級
    from
    where
    group by
    select
    distinct
    having
    order by
    limit
    1.找到表:from
    2.拿着where指定的約束條件,去文件/表中取出一條條記錄
    3.將取出的一條條記錄進行分組group by,如果沒有group by,則整體作爲一組
    4.執行select(去重)
    5.將分組的結果進行having過濾
    6.將結果按條件排序:order by
    7.限制結果的顯示條數

實例:
company.employee
員工id id int
姓名 emp_name varchar
性別 sex enum
年齡 age int
入職日期 hire_date date
崗位 post varchar
職位描述 post_comment varchar
薪水 salary double
辦公室 office int
部門編號 depart_id int

#建表
create table employee(  
id int not null unique auto_increment,  
emp_name varchar(20) not null,  
sex enum('male','female') not null default 'male',   
age int(3) unsigned not null default 28,  
hire_date date not null,  
post varchar(50),  
post_comment varchar(100),  
salary double(15,2),  
office int,  
depart_id int  
); 


#插入數據  
insert into employee(emp_name,sex,age,hire_date,post,salary,office,depart_id) values  
('dapeng','male',18,'20170301','boss',7300.33,401,1),  
('alex','male',78,'20150302','teacher',1000000.31,401,1),  
('wupeiqi','male',81,'20130305','teacher',8300,401,1),  
('yuanhao','male',73,'20140701','teacher',3500,401,1),  
('liwenzhou','male',28,'20121101','teacher',2100,401,1),  
('jingliyang','female',18,'20110211','teacher',9000,401,1),  
('jinxin','male',18,'19000301','teacher',30000,401,1),  
('成龍','male',48,'20101111','teacher',10000,401,1), 
('歪歪','female',48,'20150311','sale',3000.13,402,2),  
('丫丫','female',38,'20101101','sale',2000.35,402,2),  
('丁丁','female',18,'20110312','sale',1000.37,402,2),  
('星星','female',18,'20160513','sale',3000.29,402,2),  
('格格','female',28,'20170127','sale',4000.33,402,2), 
('張野','male',28,'20160311','operation',10000.13,403,3),   
('程咬金','male',18,'19970312','operation',20000,403,3),  
('程咬銀','female',18,'20130311','operation',19000,403,3),  
('程咬銅','male',18,'20150411','operation',18000,403,3),  
('程咬鐵','female',18,'20140512','operation',17000,403,3); 


#簡單查詢  
SELECT * FROM employee;  
SELECT emp_name,salary FROM employee; 

#避免重複  
SELECT DISTINCT post FROM employee; 

#四則運算查詢  
SELECT emp_name, salary*12 AS Annual_salary FROM employee;

where約束

  • 語法
  1. 比較運算符:> < >= <= <> !=
  2. between 80 and 100 值在80到100之間
  3. in(80,90,100) 值是80或90或100
  4. like ‘e%’
    通配符可以是%或_,
    %表示任意多字符
    _表示一個字符
  5. 邏輯運算符:在多個條件直接可以使用邏輯運算符 and or not
#1:單條件查詢      
	SELECT emp_name FROM employee          
		WHERE post='sale'; 

#2:多條件查詢      
	SELECT emp_name,salary FROM employee          
		WHERE post='teacher' AND salary>10000; 

#3:關鍵字BETWEEN AND      
	SELECT emp_name,salary FROM employee           
		WHERE salary BETWEEN 10000 AND 20000; 
    SELECT emp_name,salary FROM employee           
    	WHERE salary NOT BETWEEN 10000 AND 20000; 

#4:關鍵字IS NULL(判斷某個字段是否爲NULL不能用等號,需要用IS)      
	SELECT emp_name,post_comment FROM employee           
		WHERE post_comment IS NULL; 
    SELECT emp_name,post_comment FROM employee           
    	WHERE post_comment IS NOT NULL; 
    SELECT emp_name,post_comment FROM employee           
    	WHERE post_comment=''; 注意''是空字符串,不是null      
    ps:          
    執行          
    update employee set post_comment='' where id=2;          
    再用上條查看,就會有結果了 

#5:關鍵字IN集合查詢      
	SELECT emp_name,salary FROM employee           
		WHERE salary=3000 OR salary=3500 OR salary=4000 OR salary=9000 ; 
    SELECT emp_name,salary FROM employee           
    	WHERE salary IN (3000,3500,4000,9000) ; 
    SELECT emp_name,salary FROM employee           
    	WHERE salary NOT IN (3000,3500,4000,9000) ; 

#6:關鍵字LIKE模糊查詢      
	通配符’%’      
	SELECT * FROM employee               
		WHERE emp_name LIKE 'eg%'; 
    通配符’_’      
    SELECT * FROM employee               
    	WHERE emp_name LIKE 'al__';

group by

單獨使用GROUP BY關鍵字分組
SELECT post FROM employee GROUP BY post;
表中teacher爲一組,sale爲一組,operation爲一組。

注意:我們按照post字段分組,那麼select查詢的字段只能是post,想要獲取組內的其他相關信息,需要藉助函數

GROUP BY關鍵字和GROUP_CONCAT()函數一起使用

SELECT post,GROUP_CONCAT(emp_name) FROM employee GROUP BY post;#按照崗位分組,並查看組內成 員名     
SELECT post,GROUP_CONCAT(emp_name) as emp_members FROM employee GROUP BY post; 

GROUP BY與聚合函數一起使用

select post,count(id) as count from employee group by post;#按照崗位分組,並查看每個組有多少人

PS:
1.如果我們用unique的字段作爲分組的依據,則每一條記錄自成一組,這種分組沒有意義
2.多條記錄之間的某個字段值相同,該字段通常用來作爲分組的依據

聚合函數

強調:聚合函數聚合的是組的內容,若是沒有分組,則默認一組

實例

 SELECT COUNT(*) FROM employee; #總記錄數
 select count(1) from emploee
      
 SELECT COUNT(*) FROM employee WHERE depart_id=1;#統計depart_id爲1的      
 SELECT MAX(salary) FROM employee;      
 SELECT MIN(salary) FROM employee;      
 SELECT AVG(salary) FROM employee;      
 SELECT SUM(salary) FROM employee;      
 SELECT SUM(salary) FROM employee WHERE depart_id=3;

分組之後having過濾

執行優先級從高到低:where > group by > having

  1. Where 發生在分組group by之前,因而Where中可以有任意字段,但是絕對不能使用聚合函數。
  2. Having發生在分組group by之後,因而Having中可以使用分組的字段,無法直接取到其他字段,可以使用聚合函數

#想要得知每個部門中:薪水高於10000的人有哪些
#錯誤實例:

select post,group_concat(emp_name) from employee group by post	# 分組之後並沒有salary字段
select post,group_concat(emp_name) from employee group by post having salary > 10000;

#正確示例:你需要在分組前先把薪水高於10000的人過濾出來,然後再進行分組

select post,group_concat(emp_name) from employee where salary > 10000 group by post

order by

按單列排序

	SELECT * FROM employee ORDER BY salary;      
	SELECT * FROM employee ORDER BY salary ASC;      
	SELECT * FROM employee ORDER BY salary DESC; 

按多列排序:先按照age排序,如果年紀相同,則按照薪資排序

    SELECT * from employee          
    	ORDER BY age,          
    	salary DESC;

實例

# 正序倒序實例單字段:
select id,emp_name,salary from  employee where post='teacher' order by salary ASC;
select id,emp_name,salary from  employee where post='teacher' order by salary desc;
# 正序倒序實例多字段: 先以第一個字段進行排序,如果字段相同則以下一個字段進行排序
select id,emp_name,age,salary from  employee where post='teacher' order by age desc,salary desc;
# 示例:查詢每個部門中:薪水高於1000的人有哪些並根據部門人數進行排序操作
select post,count(emp_name) as numbers from employee where salary > 1000 group by post order by numbers ASC;

limit

# 限制輸出:limit 
select id,emp_name,salary from  employee where post='teacher' limit 3	# 輸出前3行內容 起始位置爲0

select id,emp_name,salary from  employee where post='teacher' limit 5,2	# 輸出的是第6和第7行內容,即從第六行開始輸出兩行

正則表達式:REGEXP關鍵字

實例:

select * from employee where emp_name regexp "^al"
#以al開頭的員工名字
select * from employee where emp_name regexp "ao$"
#以ao結尾的員工名字
select * from employee where emp_name regexp "m{2}"
#同時出現兩次m的員工名字
  • 小結:關於對字符串的匹配
  1. = ‘string’
  2. like 通配符
  3. regexp 正則表達式

多表查詢

  • 建表及準備數據
#建表
create table department(  
id int,  
name varchar(20)   
); 

create table employee2(  
id int primary key auto_increment,  
name varchar(20),  
sex enum('male','female') not null default 'male',  
age int,  
dep_id int  
); 

#插入數據  
insert into department values  
(200,'技術'),  
(201,'人力資源'),  
(202,'銷售'),  
(203,'運營'); 
insert into employee2(name,sex,age,dep_id) values  
('egon','male',18,200),  
('alex','female',48,201),  
('wupeiqi','male',38,201),  
('yuanhao','female',28,202),  
('liwenzhou','male',18,200),  
('jingliyang','female',18,204)  ;

  • 語法
    重點:外鏈接語法
    格式:
    SELECT 字段列表
    FROM 表1 INNER|LEFT|RIGHT JOIN 表2
    ON 表1.字段 = 表2.字段;

  • 交叉鏈接:不適用任何匹配條件。生成笛卡爾積:第一張表n×第二張表m

MariaDB [test]> select * from employee2,department;
  • 內連接:只連接匹配的行
    查詢每個員工所在的部門(兩張表之間的關係:dep_id 和 部門id)
MariaDB [test]>  select  
employee2.id,employee2.name,employee2.age,employee2.sex,department.name 
from employee2 inner join department on employee2.dep_id=department.id; 

MariaDB [test]> select  
employee2.id,employee2.name,employee2.age,employee2.sex,department.name from  
employee2,department where employee2.dep_id=department.id;  

+‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐+  
| id | name      | age  | sex    | name         |  
+‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐+  
|  1 | egon      |   18 | male   | 技術         |  
|  2 | alex      |   48 | female | 人力資源     |  
|  3 | wupeiqi   |   38 | male   | 人力資源     |  
|  4 | yuanhao   |   28 | female | 銷售         |  
|  5 | liwenzhou |   18 | male   | 技術         |  
+‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐+
  • 外鏈接之左連接:優先顯示左表全部記錄
    左表6號員工在右表department中沒有匹配,但還是顯示。
MariaDB [test]> select employee2.id,employee2.name,department.name as depart_name from  
employee2 left join department on employee2.dep_id=department.id;  
+‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐+  
| id | name       | depart_name  |  
+‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐+  
|  1 | egon       | 技術         |  
|  5 | liwenzhou  | 技術         |  
|  2 | alex       | 人力資源     |  
|  3 | wupeiqi    | 人力資源     |  
|  4 | yuanhao    | 銷售         |  
|  6 | jingliyang | NULL         |  
+‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐+

  • 外鏈接之右連接:優先顯示右表全部記錄
    右表department中運營在左表中沒有匹配,但還是顯示。
MariaDB [test]> select employee2.id,employee2.name,department.name as depart_name from  
employee2 right join department on employee2.dep_id=department.id;  
+‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐+  
| id   | name      | depart_name  |  
+‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐+  
|    1 | egon      | 技術         |  
|    2 | alex      | 人力資源     |  
|    3 | wupeiqi   | 人力資源     |  
|    4 | yuanhao   | 銷售         |  
|    5 | liwenzhou | 技術         |  
| NULL | NULL      | 運營         |  
+‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐+

  • 全外連接:顯示左右兩個表全部記錄
MariaDB [test]> select * from employee2 left join department on employee2.dep_id = department.id      
‐> union      
‐> select * from employee2 right join department on employee2.dep_id = department.id;  
+‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐+  
| id   | name       | sex    | age  | dep_id | id   | name         |  
+‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐+  
|    1 | egon       | male   |   18 |    200 |  200 | 技術         | 
|    5 | liwenzhou  | male   |   18 |    200 |  200 | 技術         | 
|    2 | alex       | female |   48 |    201 |  201 | 人力資源     |  
|    3 | wupeiqi    | male   |   38 |    201 |  201 | 人力資源     |  
|    4 | yuanhao    | female |   28 |    202 |  202 | 銷售         | 
|    6 | jingliyang | female |   18 |    204 | NULL | NULL         |  
| NULL | NULL       | NULL   | NULL |   NULL |  203 | 運營         | 
+‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐+ 
#注意 union與union all的區別:union會去掉相同的紀錄

  • 符合條件連接查詢
    示例1:以內連接的方式查詢employee和department表,並且employee表中的age字段值必須大於25,即找出年齡 大於25歲的員工以及員工所在的部門
select employee2.name,department.name from employee2 inner join department      
on employee2.dep_id = department.id      
where employee2.age > 25; 

示例2:以內連接的方式查詢employee和department表,並且以age字段的升序方式顯示

select * from employee2 inner join department      
on employee.dep_id = department.id          
order by employee2.age asc;

總結:對多表進行查詢的時候,先把連接查詢寫下來(找到多張表之間的對應關係),然後根據查詢記錄可以當成單表進行操作(關鍵字:where ORDER BY …)

子查詢

1:子查詢是將一個查詢語句嵌套在另一個查詢語句中。
2:內層查詢語句的查詢結果,可以爲外層查詢語句提供查詢條件。
3:子查詢中可以包含:IN、NOT IN、ANY、ALL、EXISTS 和 NOT EXISTS等關鍵字
4:還可以包含比較運算符:= 、 !=、> 、<等

  • 帶IN關鍵字的子查詢
#查詢平均年齡在25歲以上的部門名  
select id,name from department      
where id in           
(select dep_id from employee2 group by dep_id having avg(age) > 25); 

#查看技術部員工姓名  
select name from employee2`      
where dep_id in           
(select id from department where name='技術'); 

#查看不足1人的部門名(子查詢得到的是有人的部門id)  
select name from department where id not in (select distinct dep_id from employee);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章