XMRHRX
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee
    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee
  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    --- title: SQL 3/4 進階SQL --- ## 進階 SQL ### 2020/3/24 <!-- https://dbdiagram.io/home 3.進階 SQL & SQL 查詢練習/挑戰 INNER JOIN UNION SELECT 畫diagram --> --- ## 課程上到這裡 ### 是不是覺得SQL好像也沒什麼特別的?(笑 NOTE:今天來說說SQL相對傷腦的、需要更像數學集合一樣思維的用法。 ---- ### 如果我今天想要... ##### 用A表內容更新B表.. ##### 刪掉C表內,有出現在D表的內容... NOTE:這種表表之間的複雜關係,讓SQL變得很好玩又很傷腦 --- ### INNER JOIN 把令一張表根據條件,加進來 ``` SELECT ... FROM <table_A> [WHERE <condition>] INNER JOIN <table> ON <condition> ``` NOTE: 這個範例可以讓查詢的結果,是兩個表根據條件合成的結果,因為是查詢,所以它只是顯示而已,並不是真的讓其中一張表變成你看到的樣子。當然更新之類的其他功能也可以用到INNER JOIN,而除了INNSER JOIN還有其他形式的JOIN。 ---- ![](https://i.imgur.com/kpmdm8L.png) ---- ``` SELECT customers.Name, orders.Order_No FROM customers INNER JOIN orders ON customers.C_Id=orders.C_Id; ``` ![](https://i.imgur.com/yGSskrL.png) 結論:符合條件的才顯示(O_Id對應不到使用者,所以沒顯示) --- ### LEFT JOIN ``` SELECT <col1>[, <col2>...] FROM <table 1> LEFT JOIN <table 2> ON <tableX>.<col?>=<tableY>.<col?>; ``` ---- ![](https://i.imgur.com/hAD3SGS.png) ---- ``` SELECT customers.Name, orders.Order_No FROM customers LEFT JOIN orders ON customers.C_Id=orders.C_Id; ``` ![](https://i.imgur.com/lQSjtth.png) 結論:列出所有左表的資料 (每個符合條件的都會列出一次,右表如果不符合的也列出,但是對應資料會空著) NOTE: 這邊可以注意到,因為不同表之間的欄位可以能會一樣,像是這個範例裡面,兩張表都有C_Id,所以會加上表的名稱,才有辦法知道你在指定哪一張表的C_Id。 --- ### RIGHT JOIN ``` SELECT col1[, col2] FROM <table1> RIGHT JOIN <table2> ON <tableX>.<col1>=<tableY>.<col2> ``` ---- ![](https://i.imgur.com/csKoeYq.png) ---- ``` SELECT customers.Name, orders.Order_No FROM customers RIGHT JOIN orders ON customers.C_Id=orders.C_Id; ``` ![](https://i.imgur.com/WYTvWyk.png) 結論:列出所有右表的資料 (每一筆都列出,對應不到的左表就空著,基本上和LEFT JOIN的差別只是左右表定義) NOTE: 可以注意到,範例的語法跟LEFT JOIN是一樣的,兩個差別在於RIGHT JOIN是保留右邊表的所有資料,然後把符合條件的從左放進來,沒有就空著;而LEFT JOIN是保留左邊表的所有資料,然後把符合條件的從右表放進來,沒有就空著 這邊要能理解,為什麼LEFT JOIN時,明明原本表只有3筆資料,最後顯示確有5筆,但RIGHT JOIN時,數量就跟原本表一樣,但是又說LEFT和RIGHT只是左右表定義相反而已。 因為LEFT的條件有多個符合,所以分別列出多個,但是RIGHT都是只有一個或是沒有符合,所以有剛好一樣 --- ### try try > 建立剛剛範例的表格,並且試試上面的各種JOIN方式 --- ### UNION ~~一個我根本沒有用過得功能 不對等等這好像是我注入時候有用過~~ 擷取表之間,列出指定欄位,不重複的值 ``` SELECT <col> FROM <table> UNION SELECT <col> FROM <table>; ``` ---- ### 概念上 ![](https://i.imgur.com/tfylXPA.png) NOTE: UNION 指令的目的是將兩個 SQL 語句的結果合併起來。從這個角度來看, UNION 跟 JOIN 有些許類似,因為這兩個指令都可以由多個表格中擷取資料。 UNION 的一個限制是兩個 SQL 語句所產生的欄位需要是同樣的資料種類。另外,當我們用 UNION 這個指令時,我們只會看到不同的資料值 (類似 SELECT DISTINCT)。 講到這裡,你們可能覺得台上那個在公鯊ㄒ... ---- #### 公鯊ㄒ... ![](https://i.imgur.com/ssNiZDk.png) 我們來看看範例 ---- table: ![](https://i.imgur.com/0jPz2nl.png) Query: ``` SELECT Txn_Date FROM Store_Information UNION SELECT Txn_Date FROM Internet_Sales; ``` NOTE:可以先想想看結果。 ---- result: ![](https://i.imgur.com/qgIP9HL.png) 結果:從兩張表,列出不重複的Date (皆為同類型資料) --- ### 更多condition ---- ### LIKE % ``` 1. WHERE `username` LIKE "Japari" =>"Japari" 2. WHERE `username` LIKE "Ja%" =>"Japari", "JapariPark", "Japan", "JaKaMi" ``` NOTE: SQL除了基本的= > < !=,在字串的比對上還多了很多特別的用法,而這些特別的用法通常都是用LIKE --- ### 大挑戰時間!:D https://www.hackerrank.com/domains/sql?filters%5B.%28%29%5D%5B%5D=select&filters%5Bsubdomains%5D%5B%5D=join <!-- https://www.hackerrank.com/challenges/african-cities/problem SELECT `CITY`.`name` FROM `CITY` INNER JOIN `COUNTRY` ON `CITY`.`CountryCode`=`COUNTRY`.`Code` AND `COUNTRY`.`CONTINENT` LIKE 'Africa' -->

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully