SQl Server 錶鏈接查詢

來源:互聯網
上載者:User

標籤: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 錶鏈接查詢

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.