標籤:nbsp 串連 left join color sel 比較 實值型別 c# server
之前漏下了,這裡補一偏
select * from student,score ——笛卡爾積
可以想想成c#裡面的多維函數的樣子,列印時每一張表的每一條資料都會連帶著第二張表的所有資料
兩個表的串連:
第一種方法(比較常用)
select student.sno, sname, degree
from student,score ----當查詢的列名兩個表中都有時要說明查的是那個表中的列 用 表明點列名(student.sno)
where student.sno=score.sno -- 連個表共有的那一列,這樣就可以說明兩個表建立聯絡
第二種方法
select cno, (select sname from student where student.sno=score.sno),degree from score 套路是一樣的,位置不相同
第三種方法
select student.sno ,sname,cno,degree from student join score on student.sno=score.sno
--- //inner join(預設) //left join(以左表為主表) //right join(以右表為主表)
三個表的串連
第一種方法(跟上面兩個表一樣)
select student.sno, sname, cno, degree, name ---如果degree+10, name+‘同學’ 結果是:成績數值增加10名字後面加上同學,數實值型別就會計算,字串類就會拼接
from student,score,course ----當查詢的列名不只存在1個表中都有時要用 表明點列名(student.sno)說明是那一個表中的
where student.sno=score.sno
and score.cno=course.cno
第二種方法
select student.sno, sname, cno, degree, name
from student join score
on student.sno=course.sno
join course
on score.cno=course.cno
縱連結 資料類型要一致
兩個表之間用 union
舉個栗子:
select tname 名字,tsex 性別,tbirthday 生日 from teacher
union
select sname ,ssex,sbirthday from student
SQl Server 錶鏈接查詢