MySQL內連線排序

2021-08-28 06:00:19 字數 2425 閱讀 3535

#student1(學生表),grade1(年級表),result(考試成績表),subject(科目表)都在上乙個部落格

#查詢參加考試的同學資訊(學號 姓名 科目號 成績)

select s.studentno,studentname,subjectno,studentresult

from student1 s

inner join result r

on s.studentno = r.studentno

#查詢參加考試的同學資訊(學號 姓名 科目名稱 成績)

select s.studentno,studentname,subjectname,studentresult

from student1 s

inner join result r

on s.studentno = r.studentno

inner joinsubjectsub

on r.subjectno=sub.subjectno

#查詢學學員及所屬的年級(學號 學生姓名 年級名)

select studentno as 學號,studentname as 學生姓名,nameas 年級

from student1 s

inner join grade1 g

on s.gradied1=g.id

#查詢科目以及所屬的年紀(科目名稱 年級名稱)

select subjectname as 科目名稱,nameas 年級名稱

fromsubjectsub

inner join grade1 g

on sub.gradeid=g.id

#查詢(大學數學)的所有同學的考試結果(學號 學生姓名 科目名稱 成績)

#按照成績由高到低排序

select s.studentno,studentname,subjectname,studentresult

from student1 s

#先內連線學生表和考試成績表

inner join result r

on s.studentno = r.studentno

#再內連線考試成績表和科目表

inner joinsubjectsub

on r.subjectno=sub.subjectno

where subjectname=『大學數學』

#排序,預設為asc公升序 desc為降序

order by studentresult desc,studentno

#常見錯誤:order by studentresult,studentno desc

#desc 和asc只修飾附近 studentresult預設公升序排列 studentno 降序排列

#查詢(大學數學)的所有同學的考試結果(學號 學生姓名 科目名稱 成績)

#按照成績由高到低排序,分頁顯示

#分頁這裡很重要,(使用者體驗,網路傳輸,查詢的壓力會減少)

select s.studentno,studentname,subjectname,studentresult

from student1 s

inner join result r

on s.studentno = r.studentno

inner joinsubjectsub

on r.subjectno=sub.subjectno

where subjectname=『大學數學』

order by studentresult desc

#從哪條記錄開始(num1,num2) num1起始行 num2要顯示幾條

#資料記錄起始行從0開始

#起始行從當前頁減一乘要顯示的行 num1=(當前頁-1)×num2

#limit 0,1; #第一頁

#limit 1,1; #第二頁

limit 2,1; #第三頁 等同於 limit 5 offset 0;

#查詢 大學數學 課程前3名且分數大於94分的學生資訊(學號,姓名,課程名,分數)

select s.studentno as 學號,studentname as 姓名,subjectname as 課程名,studentresult as 分數

from student1 s

inner join result r

on s.studentno=r.studentno

inner joinsubjectsub

on r.subjectno=sub.subjectno

where subjectname=『大學數學』 and studentresult>94

order by studentresult desc

limit 0,10;

mysql連線操作 MySQL內連線操作

筆記 實現內連線 以運算元據庫tb train cpp2和tb train cpp22為例 desc tb train cpp2 desc tb train cpp22 資料表tb train cpp2中的字段country與tb train cpp22中的字段cname對應 可實現內連線 sele...

mysql 內連線查詢

例7.46 在fruits表和suppliers表之間使用內連線查詢,查詢之前,檢視兩個表的結構,select suppliers.s id,s name,f name,f price from fruits suppliers where fruits.s id suppliers.s id 例7...

mysql 內連線 左連線 右連線

記錄備忘下,初始資料如下 drop table ifexists t demo product create table ifnot exists t demo product proid int 20 proname varchar 20 price int 10 primary key proi...