LeetCode MySQL-584. Find Customer Referee

    LeetCode MySQL

    Write an SQL query to report the names of the customer that are not referred by the customer with id = 2.
    Return the result table in any order.

    寫一個 SQL Query來報告,沒有被客戶 referee_id = 2 推薦的「name」,並回傳任意順序的結果。
    

    Table: Customer

    +-------------+---------+
    | Column Name | Type    |
    +-------------+---------+
    | id          | int     |
    | name        | varchar |
    | referee_id  | int     |
    +-------------+---------+
    id is the primary key column for this table.
    Each row of this table indicates the id of a customer, their name, and the id of the customer who referred them.
    

    Example 1:

    Input: 
    Customer table:
    +----+------+------------+
    | id | name | referee_id |
    +----+------+------------+
    | 1  | Will | null       |
    | 2  | Jane | null       |
    | 3  | Alex | 2          |
    | 4  | Bill | null       |
    | 5  | Zack | 1          |
    | 6  | Mark | 2          |
    +----+------+------------+
    Output: 
    +------+
    | name |
    +------+
    | Will |
    | Jane |
    | Bill |
    | Zack |
    +------+
    

    Solution:
    1. 選擇標題「name」
    2. 來自於 Customer 的 table
    3. 設定條件「referee_id 不等於 2」
    (ex.注意需列入null的情況)

    Code.1:

    SELECT name
        FROM Customer
    WHERE referee_id IS NULL OR referee_id != 2;
    

    Code.2:

    SELECT name
        FROM Customer
    WHERE referee_id IS NULL OR referee_id <> 2;
    

    newCustomer