mysql關聯查詢和聯集查詢

來源:互聯網
上載者:User

標籤:

一、內聯方式

   1.傳統關聯查詢

     "select * from students,transcript where students.sid=transcript.sid and transcript.total>600 and transcript.date=2015-6";

     上面是查詢出在2015-6月,月考中總成績超過600的學生資訊。where的條件有三個,要看出是哪些是關聯條件,哪些是查詢過濾還是挺簡單,若是多個表多個查詢條件那麼就不是那麼容易了

   2.JOIN...ON

      "select * from students JOIN transcript ON(students.sid=transcript.sid) where transcript.total>600 and transcript.date=2015-6";

     這樣能很容易看出ON後面括弧即關聯條件,where之後為過濾條件一目瞭然。文法中ON後的()不是必須的,但是為了代碼風格建議添加。

 3.JOIN...USING

   "select * from students JOIN transcript USING(sid) where transcript.total>600 and transcript.date=2015-6";

    這個時候括弧就是必須的了。這種寫法很好,輸入更少的單詞,查詢效能上沒有太大的差異,但是JOIN...ON和傳統關聯查詢查詢時sid會出現二次。

 4.LEFT OUTER JOIN...ON

  "select * from students LEFT OUTER JOIN transcript USING(sid) where transcript.date=2015-6";

  選擇表students所有資料,並將transcript的資料加到students表中,若transcript中不符合條件,就不用加入結果表中,並且NULL表示

 5.RIGHT OUTER JOIN...ON

  "select * from students RIGHT OUTER JOIN transcript USING(sid) where transcript.date=2015-6";

  選擇表transcript所有資料,並將students的資料加到students表中,若students中不符合條件,就不用加入結果表中,並且NULL表示

 6.FULL OUTER JOIN...ON

  "select * from students FULL OUTER JOIN transcript USING(sid) where transcript.date=2015-6";

  選擇表student、transcript所有資料,若students,students中不符合條件,就不用加入結果表中,並且NULL表示

mysql關聯查詢和聯集查詢

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.