如何使用WHERE语句和COUNT(*)= 1选择行

时间:2019-02-17 00:02:36

标签: mysql sql count where-clause

我正在尝试将WHERE子句与HAVING COUNT(*)子句组合在一起。但是我无法获得想要的结果。

我想查找仅下了一个订单且订单为"inactive"的所有客户。换句话说,我正在尝试从订单表中选择customer_name值的计数为“ 1”且订单状态等于"inactive"的行。

我有这张桌子:

orders_tbl
+----+--------------+-------------+---------+-------+
| ID |    cust_name |      status | item_no | price |
+----+--------------+-------------+---------+-------+
|  1 |        Scott |      active |       4 |   2.0 |
+----+--------------+-------------+---------+-------+
|  2 |        James |      active |       2 |   4.0 |
+----+--------------+-------------+---------+-------+
|  3 |         Eric |    inactive |       3 |   8.0 |
+----+--------------+-------------+---------+-------+
|  4 |        Polly |      active |       3 |   2.0 |
+----+--------------+-------------+---------+-------+
|  5 |        Peggy |    inactive |       6 |   4.0 |
+----+--------------+-------------+---------+-------+
|  6 |         Earl |    inactive |       1 |   5.0 |
+----+--------------+-------------+---------+-------+
|  7 |        Billy |      active |       4 |   2.0 |
+----+--------------+-------------+---------+-------+
|  8 |        Peggy |    inactive |       5 |   4.0 |
+----+--------------+-------------+---------+-------+
|  9 |        Jenny |    inactive |       4 |   8.0 |
+----+--------------+-------------+---------+-------+
| 10 |        Polly |      active |       2 |   2.0 |
+----+--------------+-------------+---------+-------+
| 11 |        Scott |    inactive |       2 |   4.0 |
+----+--------------+-------------+---------+-------+
| 12 |        James |    inactive |       1 |   8.0 |
+----+--------------+-------------+---------+-------+

我想要每个仅具有一个订单的客户的名称,并且该订单是“无效”的。从上表中,我想要这些结果:

+----+--------------+-------------+---------+-------+
| ID | cust_name    |     status  | item_no | price |
+----+--------------+-------------+---------+-------+
|  3 |         Eric |    inactive |       3 |   8.0 |
+----+--------------+-------------+---------+-------+
|  6 |         Earl |    inactive |       1 |   5.0 |
+----+--------------+-------------+---------+-------+
|  9 |        Jenny |    inactive |       4 |   8.0 |
+----+--------------+-------------+---------+-------+

我尝试了此查询,以及使用WHERE和COUNT()的许多变体:

SELECT ID, cust_name, status, item_no, price, COUNT(*) 
FROM orders_tbl
WHERE status = 'inactive'
GROUP BY cust_name
HAVING COUNT(*)<2;

以上查询产生的结果接近我想要的结果。但是我得到的客户只有一个"inactive"记录,即使他们有一个或多个有效记录也是如此。我得到的结果是:

orders_tbl
+----+--------------+-------------+---------+-------+
| ID |    cust_name |      status | item_no | price |
+----+--------------+-------------+---------+-------+
|  3 |         Eric |    inactive |       3 |   8.0 |
+----+--------------+-------------+---------+-------+
|  5 |        Peggy |    inactive |       6 |   4.0 |
+----+--------------+-------------+---------+-------+
|  6 |         Earl |    inactive |       1 |   5.0 |
+----+--------------+-------------+---------+-------+
|  9 |        Jenny |    inactive |       4 |   8.0 |
+----+--------------+-------------+---------+-------+
| 11 |        Scott |    inactive |       2 |   4.0 |
+----+--------------+-------------+---------+-------+
| 12 |        James |    inactive |       1 |   8.0 |
+----+--------------+-------------+---------+-------+

1 个答案:

答案 0 :(得分:2)

一种解决方案是使用聚合,并将逻辑移至HAVING子句。以下查询可以为您提供满足条件的客户名称::

SELECT cust_name
FROM mytable
GROUP BY cust_name
HAVING COUNT(*) = 1 AND MAX(status) = 'inactive'

返回:

| cust_name |
| --------- |
| Earl      |
| Eric      |
| Jenny     |

如果您还想查看订单,则可以将其变成子查询:

SELECT *
FROM mytable
WHERE cust_name IN (
    SELECT cust_name
    FROM mytable
    GROUP BY cust_name
    HAVING COUNT(*) = 1 AND MAX(status) = 'inactive'
)

结果:

| ID  | cust_name | status   | item_no | price |
| --- | --------- | -------- | ------- | ----- |
| 3   | Eric      | inactive | 3       | 8     |
| 6   | Earl      | inactive | 1       | 5     |
| 9   | Jenny     | inactive | 4       | 8     |

Demo on DB Fiddle


从MySQL 8.0开始,窗口函数使其变得更加容易和高效。您可以内联它们以按客户计算订单总数,以及无效订单的数量;然后只适合两个值均为1的记录:

SELECT * FROM (
    SELECT 
        t.*, 
        COUNT(*) OVER(PARTITION BY cust_name) cnt_total, 
        SUM(status = 'inactive') OVER(PARTITION BY cust_name) cnt_inactive
    FROM mytable t
 ) x WHERE cnt_total = 1 AND cnt_inactive = 1;

Demo on DB Fiddle

相关问题