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