資料庫SQL語句 單錶多表連線查詢例項

2021-08-21 11:56:27 字數 1413 閱讀 1319

查詢所有學生語文科目成績,按成績高低排名

select a.id,a.names,b.score from student a join scores b on a.id=b.student_id where b.subject_id='yw' order by score desc

union all 查詢

select * from scores

union all //並起來,行數與列數必須相同

select * from scores2

select student_id,subject_id,score from scores

union all

select student_id,subject_id,score from scores2

錯誤查詢,列不相同,如:

select * from grade

union all

select * from subject

必須保證列數相同,且資料型別一樣

select id from grade

union all

select id from subject

查詢2班所有學生數學成績》60分的記錄

select a.id,a.names,b.score from student a join scores b on a.id=b.student_id where b.subject_id='sx' and a.grade_id='02' and b.score>60

查詢所有學生資訊,顯示學生姓名,性別,年齡,班級名稱,****,住址

select a.names,case a.*** when 1 then '男' when 2 then '女' else '錄入錯誤' end as ***,year(current //當前年份date)-year(date(a.birthday)//轉換成日期型別) as age,b.names,a.mobile,a.address from student a join grade b on a.grade_id=b.id

查詢所有學生成績,按照科目排序,沒有成績的學生分數為0

select c.id,c.names,c.subjectid,coalesce(d.score,0) from (select a.id,a.names,b.id as subjectid from student a,subject b) c left join scores d on c.id = d.student_id and c.subjectid= d.subject_id

order by c.subjectid

//coalesce(c.score,備用的字段)判斷為備用的

資料庫多表查詢SQL語句

最近在做 進銷存業務系統 作為專案組成員的一部分,我負責統計查詢,簡單說就是多表查詢,網上有很多,但是很明確的卻沒有,所以讓我這個初學者了費,最終還是搞出來了,在這裡也和大家共享一下成果和其中出現的問題,高手可繞道,不求點讚,只為方便大家。下面開始 我一共建了4個表,分別是supplier purc...

SQL資料庫連線語句

一般的遠端訪問的寫成這樣 data source ip initial catalog 資料庫名 userid 使用者名稱 password 密碼 本地訪問的寫成這樣 data source local initial catalog 資料庫名 userid 使用者名稱 password 密碼 如果...

資料庫 資料庫基礎5 表連線 多表連線

2 多表連線 表的數量 2 2 外部連線 前提 有時需要的資料不止在一張表中,需要多個表做結合的查詢就可以用表連線實現 1 第一種 where連線 select 表名1.列名1,表名2.列名1,表名1.列名2,表名2.列名2 from 表名1,表名2 where 表名1.列名1 表名2.列名1 2 ...