在Mysql環(huán)境下對數(shù)據(jù)進(jìn)行增刪改查的操作方法
一、插入數(shù)據(jù):
insert into 表名 [(字段名)] values (字段對應(yīng)的值1,字段對應(yīng)的值2,…)[,(字段對應(yīng)的值1,字段對應(yīng)的值2,…)];
insert into students (id,name,age,height,gender,cls_id,is_delete) values (0,'小明',18,180.00,2,1,0)
在學(xué)生表中插入“小明”數(shù)據(jù)的效果
二、修改數(shù)據(jù):
update 表名 set 字段名1=新的數(shù)據(jù)值,字段名2=新的數(shù)據(jù)值 [where 條件];
UPDATE students SET name= '鄧超';
將所有學(xué)生的姓名改成鄧超的效果
三、刪除數(shù)據(jù):
1、delete from 表名;
----------刪除表里的數(shù)據(jù),但是表仍然存在
delete from 表名 [where 條件];------------根據(jù)條件進(jìn)行刪除表里的數(shù)據(jù)
DELETE FROM students where id = 1;
delete from刪除可以加條件
2、truncate table 表名;
---------清空表里的數(shù)據(jù),但表仍然存在,而且不能加條件
TRUNCATE TABLE students where id = 1;
truncate table刪除數(shù)據(jù)會報(bào)錯(cuò)
四、數(shù)據(jù)查詢:
1、基本查詢:
①查詢?nèi)孔侄蔚娜繑?shù)據(jù):
select * from 表名;
select * from students;
查詢所有學(xué)生信息
②查詢部分字段的全部數(shù)據(jù):
select 字段名1,字段名2…from 表名;
select name,gender from students;
查詢所有學(xué)生的姓名,性別
③根據(jù)條件查詢數(shù)據(jù):
elect * from 表名 where 條件;
select name,gender from students where id = 1;
查詢id為1學(xué)生的姓名,性別
④多個(gè)條件的查詢:
條件與條件之間可以用and、or、in、between…and…來進(jìn)行條件的連接
select * from students where gender='女' and cls_id=2;
查詢性別為女并且在2班的學(xué)生的信息
⑤模糊查詢:
select * from 表名 where 字段名 like ‘值’;----------% _
select * from students where name like '小%';
查詢名字里面包含’小’的學(xué)生的信息
⑥去重:
select distinct 字段名 from 表名;
select distinct gender from students;
查詢性別有幾種分類
⑦排序: 按照單個(gè)字段排序:
select * from 表名 order by 字段名 asc/desc;(asc升序-默認(rèn),desc降序)
select * from students order by height;
將學(xué)生的身高按照升序排列
按照多個(gè)字段排序:
select * from 表名 order by 字段名1 asc/desc,字段名2 asc/desc;
select * from students order by height,age;
將學(xué)生的身高、年齡按照升序排列
有條件的排序:
select * from 表名 where 條件 order by 字段名 asc/desc;
select * from students where age = 18 order by height;
將年齡為18歲的學(xué)生按照身高升序排列
⑧限制查詢結(jié)果的數(shù)量:
limit
select * from students limit 2;
只看前2條學(xué)生信息
2、連接查詢:
(涉及到兩個(gè)表以上,在查詢的時(shí)候至少要有一個(gè)必備的連接條件,這個(gè)必備的條件就是兩個(gè)表共有的那個(gè)字段相等,而且這個(gè)字段一定在一個(gè)表里是主鍵,在另一個(gè)表里是外?。?/p>
①內(nèi)連接
顯示內(nèi)連接:select 字段 from 表名1 inner join 表名2 on 兩個(gè)表連接的條件 [where 條件];
select s.name,c.name from students s inner join classes c on s.cls_id=c.id;
查看學(xué)生所在班級
隱式內(nèi)連接:select 字段 from 表名1,表名2 where 兩個(gè)表連接的條件 [and 其他查詢的條件];
select s.name as ‘名字',c.name as ‘班級' from students s, classes c where s.cls_id = c.id;
查看學(xué)生所在班級
②外連接
左外連接:select 字段 from 表名1 left join 表名2 on 兩個(gè)表連接的條件 [where 條件];------左表的數(shù)據(jù)全部查詢出來,右表符合條件的查詢出來
select c.name,t.name from classes c left join teachers t on c.teacher_id = t.id;
查看老師所在班級
右外連接:select 字段 from 表名1 right join 表名2 on 兩個(gè)表連接的條件 [where 條件];------右表的數(shù)據(jù)全部查詢出來,左表符合條件的查詢出來
select c.name,t.name from classe c right join teachers t on c.teacher_id = t.id;
查看老師所在班級
3、聚合函數(shù)查詢:
①count()-計(jì)數(shù)
select count(*) as '學(xué)生總數(shù)' from students;
查詢班級有多少同學(xué)
②sum()-求和
select sum(height) as '身高之和' from students;
查詢班級學(xué)生的身高之和
③max()-最大值
select max(height) as '最高身高' from students;
查詢班級學(xué)生的最高身高
④min()-最小值
mysql> select min(height) as '最矮身高' from students;
查詢班級學(xué)生的最矮身高
⑤avg()-平均值
select avg(height) as '平均身高' from students;
查詢班級學(xué)生的平均身高
⑥select 聚合函數(shù)名(字段名) from 表名 [where 條件];
SELECT AVG(height) AS '1班平均身高' FROM students WHERE cls_id = 1;
查詢1班學(xué)生的平均身高
⑦select 分組的字段名,聚合函數(shù)名(字段名) from 表名 [group by 分組的字段名];
SELECT cls_id AS class_id, COUNT(*) AS student_count, AVG(age) AS average_age, MAX(height) AS max_height, MIN(height) AS min_height FROM students GROUP BY cls_id;
按班級分組查詢每個(gè)班級的學(xué)生人數(shù)、平均年齡、最高身高和最低身高
4、子查詢:
查詢嵌套查詢
①子查詢的結(jié)果只有一個(gè)值
select * from 表名 where 字段名=(select 字段名 from 表名);
select * from students where cls_id = (select cls_id from students where name = '劉德華');
查看劉德華同學(xué)的所在班級的所有同學(xué)
②子查詢的結(jié)果有多個(gè)值,等于其中的任意一個(gè)值
select * from 表名 where 字段名=any(select 字段名 from 表名);
select * from students where cls_id = any(select id from classes where teacher_id = (select id from teachers where name='趙老師'));
查看趙老師所帶的學(xué)生信息
③子查詢的結(jié)果有多個(gè)值,大于所有值
select * from 表名 where 字段名>all(select 字段名 from 表名);
select * from students where cls_id >= all(select id from classes where teacher_id = (select id from teachers where name='趙老師'));
查看學(xué)生所在班級
④子查詢?nèi)绻胁樵兊慕Y(jié)果,外查詢就執(zhí)行
select * from 表名 where exists (select 字段名 from 表名);
select * from classes where exists (select * from teachers where name='李老師');
查看存在李老師的班級表
到此這篇關(guān)于在Mysql環(huán)境下對數(shù)據(jù)進(jìn)行增刪改查的文章就介紹到這了,更多相關(guān)Mysql數(shù)據(jù)增刪改查內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
新建一個(gè)MySQL數(shù)據(jù)庫的簡單教程
這篇文章主要介紹了新建一個(gè)MySQL數(shù)據(jù)庫的簡單教程,是MySQL入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2015-05-0564位Win10系統(tǒng)安裝Mysql5.7.11的方法(案例詳解)
小編在安裝64位Win10系統(tǒng)的mac book筆記本上用mysql-installer-community-5.7.11.0安裝Mysql5.7.11,在配置mysql server時(shí)老是卡住,報(bào)錯(cuò)。下面小編把安裝方法分享給大家,供大家參考2016-08-08MySQL 8.0 新特性之檢查約束的實(shí)現(xiàn)
這篇文章主要介紹了MySQL 8.0 新特性之檢查約束的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12MySQL所支持的數(shù)據(jù)類型與表字段約束類型的學(xué)習(xí)教程
這篇文章主要介紹了MySQL所支持的數(shù)據(jù)類型與表字段約束類型的學(xué)習(xí)教程,是MySQL入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2015-12-12