SELECT子句:用來指定查詢返回欄位,星號(*)表示返回所有欄位 SELECT [DISTINCT]*|欄位列表 #DISTINCT 用來過濾重覆數據 FROM子句:用來指定數據來源的表 FROM <表名> WHERE子句:用來定義查詢返回數據的條件 WHERE 查詢條件 GROUP BY子句:用 ...
- SELECT子句:用來指定查詢返回欄位,星號(*)表示返回所有欄位
SELECT [DISTINCT]*|欄位列表 #DISTINCT 用來過濾重覆數據
- FROM子句:用來指定數據來源的表
FROM <表名>
- WHERE子句:用來定義查詢返回數據的條件
WHERE 查詢條件
- GROUP BY子句:用來指定查詢結果的分組條件
GROUP BY 分組欄位 [HAVING 分組條件]
- ORDER BY子句:用來給指定欄位排序,ASC(升序)可省略,DESC(降序)
ORDER BY 排序欄位 [ASC|DESC] #ASC 為升序|DESC為降序
- LIMIT子句:用來限制select語句返回的記錄數
LIMIT [初始位置],記錄數
舉例說明:
以student、course、score三張表示範, 三張表的創建語句如下
-- -- Database: stuInfo -- CREATE DATABASE stuInfo DEFAULT CHARACTER SET utf8; USE stuInfo; -- -- 表的結構 student /*學生表*/ -- CREATE TABLE student ( sNo CHAR(10) NOT NULL COMMENT '學號', sName VARCHAR(20) NOT NULL COMMENT '姓名', sex CHAR(2) NOT NULL DEFAULT '男' COMMENT '性別', birthday DATE NOT NULL COMMENT '出生日期', deptName VARCHAR(30) NOT NULL COMMENT '所屬班級', remark VARCHAR(80) COMMENT '備註', PRIMARY KEY (sNo), /*設置sNo為主鍵*/ INDEX (sName) /*設置sName為普通索引*/ ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- -- 轉存表中的數據 student -- INSERT INTO student (sNo, sName, sex, birthday, deptName, remark) VALUES ('1308013101', '陳斌', '男', '1993-03-20', '軟體131', ''), ('1308013102', '張潔', '女', '1996-02-08', '軟體131', ''), ('1308013103', '鄭先超', '男', '1994-04-25', '軟體131', ''), ('1308013104', '徐孝兵', '男', '1994-08-06', '軟體131', ''), ('1308013105', '王群', '女', '1995-03-27', '軟體131', ''), ('1309122501', '劉威', '男', '1994-07-13', '網路131', ''), ('1309122502', '沈雁斌', '男', '1994-05-28', '網路131', ''), ('1309122503', '楊群', '女', '1995-10-18', '網路131', ''), ('1309122504', '蔣維維', '男', '1994-10-19', '網路131', ''), ('1309122505', '楊璐', '女', '1995-09-26', '網路131', ''), ('1312054901', '王林林', '男', '1994-04-16', '機電131', ''), ('1312054902', '楊一超', '男', '1994-08-27', '機電131', ''), ('1312054903', '張偉', '男', '1995-01-03', '機電131', ''), ('1312054904', '田翠萍', '女', '1994-10-20', '機電131', ''), ('1312054905', '周偉', '男', '1995-09-10', '機電131', ''); -- -- 表的結構 course /*課程表*/ -- CREATE TABLE course ( cNo CHAR(5) NOT NULL COMMENT '課程編號', cName VARCHAR(30) NOT NULL COMMENT '課程名稱', credit TINYINT UNSIGNED COMMENT '學分', remark VARCHAR(100) COMMENT '備註', PRIMARY KEY (cNo), /*設置cNo為主鍵*/ UNIQUE (cName) /*設置cName為唯一索引*/ ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- -- 轉存表中的數據 course -- INSERT INTO course (cNo, cName, credit, remark) VALUES ('01001', 'C語言程式設計', 5, '電腦類專業課程'), ('01002', '數據結構', 4, '電腦類專業課程'), ('01003', 'Java程式設計', 4, '電腦類專業課程'), ('02001', '網路基礎', 3, '電腦類專業課程'), ('02002', '資料庫原理及應用', 4, '電腦類專業課程'), ('02003', '操作系統', 4, '電腦類專業課程'), ('09001', '機械設計基礎', 5, NULL), ('09002', '機械製造基礎', 4, NULL), ('09003', '機械製圖', 4, NULL); -- -- 表的結構 score /*成績表*/ -- CREATE TABLE score ( sNo CHAR(10) NOT NULL COMMENT '學號', cNo CHAR(5) NOT NULL COMMENT '課程編號', grade TINYINT UNSIGNED NOT NULL COMMENT '成績', PRIMARY KEY(sNo, cNo), /*設置sNo和cNo為複合主鍵*/ FOREIGN KEY(sNo) REFERENCES student(sNo) /*與學生表關聯*/ ON UPDATE NO ACTION ON DELETE NO ACTION, FOREIGN KEY(cNo) REFERENCES course(cNo) /*與課程表關聯*/ ON UPDATE NO ACTION ON DELETE NO ACTION ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- -- 轉存表中的數據 score -- INSERT INTO score (sNo, cNo, grade) VALUES ('1308013101', '01001', 72), ('1308013101', '01002', 56), ('1308013101', '01003', 77), ('1308013102', '01001', 85), ('1308013102', '01002', 73), ('1308013102', '01003', 90), ('1308013103', '01001', 79), ('1308013104', '01001', 82), ('1308013105', '01001', 63), ('1309122501', '02001', 84), ('1309122501', '02002', 92), ('1309122501', '02003', 71), ('1312054901', '09001', 87), ('1312054901', '09002', 90), ('1312054901', '09003', 95);三張表的創建
查詢語句如下:
-- 查詢學生表的所有欄位 SELECT * from student; -- 查詢學生表的學號,姓名,性別欄位 SELECT sNo,sName,sex from student; -- 查詢查詢學生表的學號,姓名,性別欄位,並用 as 定義欄位別名 SELECT sNo as '學號',sName as '姓名',sex as '性別' FROM student; -- 查詢學生表的所有女生信息 SELECT *from student WHERE sex='女'; -- 查詢課程表中學分大於4的課程所有信息 SELECT *FROM course WHERE credit>4; -- 查詢學生表中在1995年出生的學生信息 select *FROM student where birthday>='1995-1-1' AND birthday<='1995-12-31'; -- 查詢學生表中姓楊的信息 SELECT *FROM student where sName LIKE '楊%'; -- 查詢學生表中姓楊的姓名為兩個字的學生信息 SELECT *FROM student where sName LIKE '楊_'; -- 查詢學生表中姓楊的姓名為三個字的學生信息 SELECT *FROM student where sName LIKE '楊__'; -- 查詢學生表中姓名的第二個字為偉和先的同學信息 select *FROM student where sName LIKE '_偉%' OR sName LIKE '_先%'; -- 查詢學生表中在1995年出生的學生信息 SELECT *FROM student where birthday BETWEEN '1995-1-1' AND '1995-12-31'; -- 查詢成績表中成績在60到89之間的信息 SELECT *FROM score WHERE grade BETWEEN 60 and 89; -- 查詢學生表學號為 1308013101,1309122503,1312054904 的學生信息 SELECT *FROM student WHERE sNo in('1308013101','1309122503','1312054904'); -- 查詢課程表中標記為空的信息 SELECT *FROM course where remark is NULL; -- 查詢學生表中男生的信息並按生日降序排序 SELECT *FROM student WHERE sex='男' ORDER BY birthday DESC; -- 查詢學生表所有信息並按性別降序,學號升序排列 SELECT *FROM student ORDER BY sex DESC,sNo; -- 查詢學生表信息並按生日降序,取前五個記錄 SELECT *FROM student ORDER BY birthday DESC LIMIT 5; -- 查詢成績表中當課程號為01001的學號,課程號,成績信息,並以成績降序排列 SELECT sNo,cNo,grade FROM score WHERE cNo='01001' ORDER BY grade DESC LIMIT 1,3; -- 查詢成績表中選修的學號 並用DISTINCT去重 SELECT DISTINCT sNo FROM score; -- 用內連接 INNER JOIN 來查詢信息 SELECT student.sNo,sName,sex,cNo,grade FROM student inner JOIN score ON student.sNo=score.sNo where sex='女'; SELECT student.sNo,sName,sex,cNo,grade FROM student,score where student.sNo=score.sNo and sex='女'; -- 查詢學號為1308013101 的信息 select S.sNo,sName,sex,C.cNo,cName,grade FROM student S INNER JOIN score G ON S.sNo=G.sNo INNER JOIN course C on C.cNo=G.cNo where S.sNo='1308013101'; SELECT S.sNo,sName,sex,C.cNo,cName,grade from student S ,course C,score G where S.sNo=G.sNo and G.cNo=C.cNo and S.sNo='1308013101'; -- 查詢學號為1308013101的最高分,最低分及平均分和總分信息 SELECT max(grade) as '最高分',min(grade) as '最低分' ,avg(grade) as '平均分',sum(grade) as '總分' FROM score WHERE sNo='1308013101' ; -- 查詢學生表男生人數 SELECT count(*) as '男生人數' from student where sex='男'; -- 查詢成績表中已選修課程的人數並用DISTINCT去重 select count(DISTINCT sNo)as '已選修課程學生人數' from score; -- 分組統計男、女生人數 SELECT sex as '性別',count(*) as '學生人數' from student GROUP BY sex; -- 分組統計不同班級人數 SELECT deptName AS '班級',count(*) as '學生人數' from student GROUP BY deptName; -- 5分組查詢學生的學號、姓名、選修課程數及平均分 SELECT student.sNo as '學號',sName as '姓名', count(*)as '選修課程數',avg(grade) as '平均分' from score INNER JOIN student on score.sNo=student.sNo GROUP BY student.sNo; -- 用嵌套查詢學號為1308013101的同學在同一個班級的學生信息 SELECT *FROM student where deptName=(SELECT deptName from student where sNo='1308013101'); -- 查詢課程編號為01001的課程,且成績超過該課程平均分的學生的學號、姓名、所屬班級、課程編號、課程名稱及成績 select student.sNo,sName,deptName,course.cNo,cName,grade FROM student,score,course where student.sNo=score.sNo and score.cNo=course.cNo AND course.cNo='01001'and grade>(SELECT avg(grade)FROM score,course where course.cNo=score.cNo and course.cNo='01001'); -- 創建新數據表,查詢網路131的學生記錄,將查詢結果插入到新數據表中 CREATE TABLE tempStudent( stuNo char(10) not null PRIMARY KEY, StuName VARCHAR(20)not NULL, sex char(1), deptName varchar(30) not null ); INSERT into tempStudent(stuNo,stuName,sex,deptName) SELECT sNo,sName,sex,deptName from student where deptName='網路131'; -- 將數據結構 課程成績統一減去5 update score SET grade=grade-5 where cNo=(SELECT cNO from course WHERE cName='數據結構'); -- 查詢更新完的成績表 select *from score where cNo=(SELECT cNO from course WHERE cName='數據結構'); -- 刪除機電131的學生的成績記錄 delete from score where sNo in (SELECT sNo from student where deptName='機電131'); -- 查詢 刪除後的機電131成績記錄 SELECT *from score where sNo in (SELECT sNo from student where deptName='機電131');