SQL-LEFT JOIN,但我希望COUNT(*)只计算联接的INNER部分的结果

时间:2019-02-12 04:44:44

标签: mysql sql

我想显示每个客户的购买数量。如果他们购买了0,我想显示0。

所需的输出:

 -------------------------------------
| customer_name | number_of_purchases |
 -------------------------------------
|    Marg       |          0          |
|    Ben        |          1          |
|    Phil       |          4          |
|    Steve      |          0          |
 -------------------------------------

客户表:

 -----------------------------
| customer_id | customer_name |
 -----------------------------
|      1      |      Marg     |
|      2      |      Ben      |
|      3      |      Phil     |
|      4      |      Steve    |
 -----------------------------

购买表:

 --------------------------------------------------
| purchase_id | customer_id | purchase_description |
 --------------------------------------------------
|      1      |       2     |     500 Reams        |
|      2      |       3     |     6 Toners         |
|      3      |       3     |     20 Staplers      |
|      4      |       3     |     2 Copiers        |
|      5      |       3     |     9 Name Plaques   |
 --------------------------------------------------

我当前的查询如下:

SELECT customer_name, COUNT(*) AS number_of_purchaes 
FROM customer 
LEFT JOIN purchases ON customer.customer_id = purchases.customer_id 
GROUP BY customer.customer_id

但是,由于它是LEFT JOIN,因此查询会为没有购买商品的客户排成一行,这使他们成为COUNT(*)的一部分。换句话说,购物次数为0的客户将显示为购物次数为1,如下所示:

左联接输出:

 -------------------------------------
| customer_name | number_of_purchases |
 -------------------------------------
|    Marg       |          1          |
|    Ben        |          1          |
|    Phil       |          4          |
|    Steve      |          1          |
 -------------------------------------

我也尝试过INNER JOIN,但这导致零购买的客户根本没有显示:

INNER JOIN输出:

 -------------------------------------
| customer_name | number_of_purchases |
 -------------------------------------
|    Ben        |          1          |
|    Phil       |          4          |
 -------------------------------------

如何实现显示购买次数为0的客户的期望输出

7 个答案:

答案 0 :(得分:6)

使用count(*)代替count(purchase_id)

SELECT customer_name, COUNT(purchase_id) AS number_of_purchaes 
FROM customer 
LEFT JOIN purchases ON customer.customer_id = purchases.customer_id 
GROUP BY customer_id,customer_name

答案 1 :(得分:5)

您可以尝试这样:

样本数据:

create table customer(customer_id integer, customer_name varchar(20));

create table purchaser(purchaser_id varchar(20), customer_id integer, description varchar(20));


insert into customer values(1, 'Marg');
insert into customer values(2, 'Ben');
insert into customer values(3, 'Phil');
insert into customer values(4, 'Steve');

insert into purchaser values(1, 2, '500 Reams');
insert into purchaser values(2, 3, '6 toners');
insert into purchaser values(3, 3, '20 Staplers');
insert into purchaser values(4, 3, '20 Staplers');
insert into purchaser values(5, 3, '20 Staplers');

SELECT c.customer_id, c.customer_name, COUNT(p.purchaser_id) AS number_of_purchaes 
FROM customer c
LEFT JOIN purchaser p ON c.customer_id = p.customer_id 
GROUP BY c.customer_id;

SQL提琴:http://sqlfiddle.com/#!9/32ff0a/2

答案 2 :(得分:2)

COUNT(*)返回一个组中的项目数。这包括NULL值和重复项。

COUNT(ALL表达式)计算组中每一行的表达式,并返回非空值的数量。

CREATE table customer(customer_id integer , customer_name varchar(20));

create table purchases(purchase_id integer , customer_id integer , purchase_description varchar(30));

INSERT INTO customer ( customer_id, customer_name )
VALUES ( 1, 'Marg' )
     , ( 2, 'Ben' )
     , ( 3, 'Phil' )
     , ( 4, 'Steve' );

INSERT INTO purchases ( purchase_id, customer_id, purchase_description )
VALUES ( 1, 2, '500 Reams' )
     , ( 2, 3, '6 toners' )
     , ( 3, 3, '20 Staplers' )
     , ( 4, 3, '2 Copiers' )
     , ( 5, 3, '9 Name Plaques' );


 SELECT  c.customer_name
      , COUNT(p.purchase_id) AS number_of_purchases
FROM    customer c
        LEFT JOIN purchases p
            ON c.customer_id = p.customer_id
GROUP BY c.customer_name

答案 3 :(得分:0)

COUNT(*)对行进行计数。您要对匹配进行计数,因此从第二个表进行计数,如下所示:

select customer.customer_name , a.number_of_purchases from (
SELECT customer_id, COUNT(purchases.purchase_id) AS number_of_purchaes 
FROM customer 
LEFT JOIN purchases ON customer.customer_id = purchases.customer_id 
GROUP BY customer.customer_id) as a 
inner join customer on customer.customer_id=a.customer_id;

换句话说,LEFT JOIN在没有匹配项时返回一行。该行在NULL表中的所有列中都有一个purchases值。

答案 4 :(得分:0)

SELECT 
  customer_name, COUNT(purchase_id) AS number_of_purchases
FROM
  customer AS c
LEFT JOIN purchases AS p ON (c.cid = p.cid)
GROUP BY c.name

答案 5 :(得分:0)

使用COUNT(purchases.customer_id)代替count(*)

SELECT customer_name, COUNT(purchases.customer_id) AS number_of_purchaes 
FROM customer 
LEFT JOIN purchases ON customer.customer_id = purchases.customer_id 
GROUP BY customer.customer_id

答案 6 :(得分:0)

SELECT c.customer_name,count(p.purchase_id)number_of_purchases FROM Customer c
LEFT JOIN 
 Purchases AS p ON c.customer_id = p.customer_id
 GROUP BY c.customer_name