zoukankan      html  css  js  c++  java
  • [LeetCode#183]Customers Who Never Order

    Suppose that a website contains two tables, the Customers table and the Orders table. Write a SQL query to find all customers who never order anything.
    
    Table: Customers.
    
    +----+-------+
    | Id | Name  |
    +----+-------+
    | 1  | Joe   |
    | 2  | Henry |
    | 3  | Sam   |
    | 4  | Max   |
    +----+-------+
    Table: Orders.
    
    +----+------------+
    | Id | CustomerId |
    +----+------------+
    | 1  | 3          |
    | 2  | 1          |
    +----+------------+
    Using the above tables as example, return the following:
    
    +-----------+
    | Customers |
    +-----------+
    | Henry     |
    | Max       |
    +-----------+
    

      

    解法一:

      使用的方法是表customers和表orders左连接,并且条件是customerId为空的客户:

    SELECT NAME as Customers FROM customers as a
    LEFT JOIN orders as b on a.Id = b.CustomerId
    WHERE b.CustomerId is NULL;
    

      

    解法二:

      使用的方法是使用not in :

    SELECT Name as Custmoers FROM customers as a 
    WHERE a.Id not in 
    (SELECT b.CustomerId FROM orders as b);
    

      

                  
    申明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    安装redis
    memcached复制-repcached
    memcached一致性哈希及php客户端实现
    安装php
    安装mysql
    安装apache
    putty配色方案
    virtualbox下centos实现主宿互访
    安装memcached
    linux网络、性能相关命令
  • 原文地址:https://www.cnblogs.com/lsyb-python/p/11079686.html
Copyright © 2011-2022 走看看