1. 程式人生 > 實用技巧 >mysql語句學習總結1

mysql語句學習總結1

/*案例1*/
create table students(
id int auto_increment not null primary key comment'ID',
name varchar(30) not null comment'名字',
email varchar(50) not null comment'郵箱',
address varchar(100) null default'北京' comment'地址'
);

--插入資料
insert into students(name,email,address) values('張飛','[email protected]',default),('
關羽','[email protected]','地獄'); insert into students(name,email,address,age,birth) values('張三','[email protected]',default,56,'1986-12-5'),('李四','[email protected]','地獄',26,'1888-9-2'); insert into students(name,email,address,age,birth) values('小明','[email protected]','大同',45,'1986-12-5'),('大龍','自己看@qq.com','南京',26,'1988-9-2
'); --給表新增列 alter table students add column phone varchar(11) not null comment '電話號碼'; --新增欄位內的值 update students set phone=12338383388 where name='張飛'; update students set age=66 where name = '張飛'; update students set birth='1997-2-9' where name = '張飛'; --新增多個欄位 ALTER TABLE students ADD COLUMN age int
(3) comment '年齡', ADD COLUMN birth VARCHAR(255) comment '生日'; --新增多個欄位值 update students set age=85,birth='1993-9-9' where name='關羽'; --修改欄位中的型別 alter table students modify phone varchar(25) null ; --刪除表裡的欄位 alter table students drop column phone; --檢索欄位名並升序排列 select name,age from students order by age; --檢索案例表裡條件age欄位為21到60的所有內容按age升序排列: select * from students where age<=60 and age>=21 order by age; --檢索案例表裡條件address欄位為北京或地獄並age欄位大於等於23的所有資訊並按升序排列 select * from students where(address='北京' or address='地獄') and age >= 25 order by age; --檢索案例表裡條件age欄位不是21和50的name,age,address資訊並絳序排列: select name,age,address from students where age not in (21,50) order by age desc; --檢索案例表裡搜尋條件name欄位為張…的所有內容: select * from students where name like '%張%'; --檢索案例表裡總人數的數量,最小年齡,最大年齡,年齡總和,平均值並起別名: select count(*) as 總人數,min(age) as 最小年齡,max(age) as 最大年齡,sum(age) as 總和,avg(age) as 平均值 from students; /*案例2*/ --建立學生表student,插入6條記錄 CREATE TABLE student ( stu_id INT(10) PRIMARY KEY, stu_name VARCHAR(20)NOT NULL, sex VARCHAR(2), birth YEAR, department VARCHAR(20), addr VARCHAR(50) ); --插入資料 INSERT INTO student VALUES ( 901,'張飛', '',1985,'計算機系', '河北省涿州市'), ( 902,'關羽', '',1986,'中文系', '山西省運城市'), ( 903,'貂蟬', '',1990,'中文系', '山西省忻州縣'), ( 904,'劉備', '',1990,'英語系', '河北省涿州市'), ( 905,'小喬', '',1991,'英語系', '安徽省潛山市'), ( 906,'趙雲', '',1988,'計算機系', '河北省正定市'); --建立分數表score,插入10條記錄 CREATE TABLE score ( score_id INT(10) PRIMARY KEY AUTO_INCREMENT , stu_id INT(10) NOT NULL , c_name VARCHAR(20) , grade INT(10) ); INSERT INTO score(stu_id,c_name,grade) VALUES(901, '計算機',98), (901, '英語', 80),(902, '計算機',65),(902, '中文',88), (903, '中文',95),(904, '計算機',70),(904, '英語',92), (905, '英語',94),(906, '計算機',90),(906, '英語',85); --從student表中查詢計算機系和英語系學生的資訊 select * from student where department in ('計算機系','英語系'); select * from student where department='計算機系' or department='英語系'; --從student表中查詢年齡25~28歲的學生資訊。 select stu_id,stu_name,sex, 2016-birth AS age,department,addr from student WHERE 2016-birth BETWEEN 25 AND 28; SELECT stu_id,stu_name,sex,2016-birth AS age,department,addr FROM student WHERE 2016-birth>=25 AND 2016-birth<=28; --在student表中統計每個院系各有幾個學生 select department as 院系 , count(stu_id) as 人數 from student group by department; --查詢每個院系學生中的最高分 select max(grade),department from score left join student on score.stu_id=student.stu_id group by department; --查詢學生貂蟬的考試科目(c_name)和考試成績(grade) select stu_name,c_name,grade from score left join student on score.stu_id=student.stu_id where stu_name='貂蟬'; --查詢計算機成績低於95的學生資訊。 select stu_name,sex,birth,addr,department,c_name,grade from student left join score on score.stu_id=student.stu_id where c_name='計算機' and grade<=95; --查詢同時參加計算機和英語考試的學生的資訊。 select * from student where stu_id=any( select stu_id from score where stu_id in (select stu_id from score where c_name='計算機') and c_name='英語' ); --從student表和score表中分別查詢出學生的學號,然後合併查詢結果。 select stu_id from student union select stu_id from score; --查詢姓張或者姓趙的同學的個人資訊、院系和考試科目及成績。 select student.stu_id, stu_name,sex,birth, department,addr, c_name,grade from student,score where (stu_name like "張%" or stu_name like "趙%") and (student.stu_id=score.stu_id); --查詢山西省的學生的姓名、出生年份、院系、考試科目及成績 SELECT stu_name,birth,department, addr,c_name,grade from student,score where addr like "山西%" and student.stu_id=score.stu_id;