紅色標注為測試未通過,綠色需要重新備注
1.mysql 數據庫的常用指令
*代表所有列
查詢表內容
selecet * from stu; 查看stu表中的所有列
修改表內容
Update teacher stu sname “李軍”=”李三” 修改teacher表姓名為李軍改為李三
刪除:
Delete from 表名; 刪除 從 哪個表
查詢條件
(1)簡單查詢
select * from Info 查詢lnfo表中的所有列
備注 ... as ....
例:select Code as '代號',Name as '姓名' from Info
把lnfo表中code備注為代號,name備注為姓名
(2) 條件查詢
Where后面跟條件 條件要寫清楚 或者用or 並且用and
示例
1.查詢成績表中成績(degree)為92的信息
Select * from score where degree =”92”;
2.查詢成績表中課程號是3-245並且成績是86的信息
Select * from score where cno='3-245' and degree=”86”;
3.查詢成績表中課程號是3-245並且成績是86的信息
Select * from score where cno='3-245' or degree=”86”;
(3) 模糊查詢 like not like
示例
查找老師表中姓李的 名字為兩個字的信息
select * from teacher where name like '李%';
或者為 select * from teacher where name like "李_ _";
%代表任意多個字符 _代表一個字符
(4)排序查詢 order by 字段 排序值(desc/asc )(降序/升序)升序為系統默認可不備注
以Class降序查詢Student表的所有記錄
select * from student order by class desc;
(5)范圍查詢 關系運算符 <, >, >=, <=, !=, <>不等於
between......and...... ,在...跟....之間
查詢Score表中成績在60到80之間的所有記錄
select * from score where degree between 60 and 80;
也可表達為 select * from score where degree >60 and degree<80;
6)離散查詢 in not in
select * from student where sname in ('張三','李四');
student表中sname為張三,李四,的信息
(7)聚合函數,統計查詢
select sum(Price) from Car
#查詢所有價格之和 sum()求和
select count(Code) from Car
#查詢數據條數
select max(Code) from Car
#求最大值
select min(Brand) from Car
#求最小值
select avg(Price) from Car
#求平均值
在一個表中顯示成績總和條數最大值最小值平均數:
Select sum(degree) as”總和”,
Count(degree) as”條數”,
Max(degree) as”最大”,
Min(degree) as”最低”,
avg(degree) as”平均”from score;
(8)分頁查詢 limit 從第幾條開始,取多少條數據
#每頁顯示5條數據,取第2頁的數據
select * from student limit (pageSize-1)*5,5
(9)去重查詢distinct
select distinct cno from score;
從score表中去重cno列下內容
(10)分組查詢 group by 字段 having 條件
select count(*),cno,group_concat(degree),sum(degree) from score group by cno ;
select cno,group_concat(degree),sum(degree) from score group by cno having count(*)>3
#分組之后根據條件查詢使用having 不使用where
高級查詢
連接查詢,對列的擴展
Select * from student as stu,score as sc
where stu.sno = sc.sno and sc.sno = “103” ;
2.聯合查詢,對行的擴展
select Code,Name from Info union
select Code,Name from Nation
3.子查詢
(1)無關子查詢
外層查詢 (里層查詢)
子查詢的結果當做父查詢的條件
子查詢:select Code from Nation where Name='漢族'
父查詢:select * from Info where Nation = ''
select * from Info where Nation = (select Code from Nation where Name='漢族')
(2)相關子查詢
查詢汽車表中油耗低於該系列平均油耗的所有汽車信息
父查詢:select * from Car where Oil<(該系列平均油耗)
子查詢:select avg(Oil) from Car where Brand = '某個系列'
select * from Car a where Oil<(select avg(Oil) from Car b where b.Brand = a.Brand )