mysql 根据条件计算子表行数

发布于 2024-07-18 06:29:31 字数 1306 浏览 7 评论 0原文

我有两个表:“用户”-> "order"

TABLE: user

user_id
-----------
    u1
    u2

TABLE: order

order_id | user_id | flag
-------------------------
    o1   |    u1   |  fA
    o2   |    u2   |  fB

Y需要获取所有用户统计有多少次带有'fA'标志的订单< /strong>

我需要的结果:

user_id | orders
----------------
   u1   |   1
   u2   |   0

我尝试:

SELECT
    u.user_id,
    COUNT(o.order_id) AS orders
FROM
    `user` AS u LEFT JOIN
    `order` AS o USING (user_id)
WHERE
    o.flag IS NULL OR
    o.flag IN ('fA')
GROUP BY
    u.user_id;

但是,此查询不包括 user=u2,因为他没有带有标记 fA; 我需要 user=u2orders=0 一起出现,

也许是这样的:

SELECT
    u.user_id,
    COUNT(o.order_id IF o.flag IN('fA')) OR 0 AS count ...

表格和数据:

CREATE TABLE `user` (user_id VARCHAR(2) NULL);
CREATE TABLE `order` (order_id VARCHAR(2) NULL,user_id VARCHAR(2) NULL,flag VARCHAR(2) NULL);
INSERT INTO `user` VALUES ('u1'), ('u2');
INSERT INTO `order` VALUES ('o1','u1','fA'),('o2','u2','fB');

I have two tables: "user" -> "order"

TABLE: user

user_id
-----------
    u1
    u2

TABLE: order

order_id | user_id | flag
-------------------------
    o1   |    u1   |  fA
    o2   |    u2   |  fB

Y need obtain all users counting how many times have orders with flag 'fA'

RESULTS WHAT I NEED:

user_id | orders
----------------
   u1   |   1
   u2   |   0

I TRY:

SELECT
    u.user_id,
    COUNT(o.order_id) AS orders
FROM
    `user` AS u LEFT JOIN
    `order` AS o USING (user_id)
WHERE
    o.flag IS NULL OR
    o.flag IN ('fA')
GROUP BY
    u.user_id;

But, this query is excluding user=u2 because he don't have an order with flag fA;
I need the user=u2 appear with orders=0

Maybe something like that:

SELECT
    u.user_id,
    COUNT(o.order_id IF o.flag IN('fA')) OR 0 AS count ...

Tables and data:

CREATE TABLE `user` (user_id VARCHAR(2) NULL);
CREATE TABLE `order` (order_id VARCHAR(2) NULL,user_id VARCHAR(2) NULL,flag VARCHAR(2) NULL);
INSERT INTO `user` VALUES ('u1'), ('u2');
INSERT INTO `order` VALUES ('o1','u1','fA'),('o2','u2','fB');

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

夜还是长夜 2024-07-25 06:29:31

您可以尝试在“LEFT JOIN”的“ON”语句中使用条件,如下所示:

SELECT u.user_id, COUNT(o.user_id)
FROM user u
LEFT JOIN `order` o ON u.user_id = o.user_id AND o.flag = 'fA'
GROUP BY user_id

运行SQLFiddle 中的示例

You can try using the condition in the 'ON' statement of the 'LEFT JOIN', like this:

SELECT u.user_id, COUNT(o.user_id)
FROM user u
LEFT JOIN `order` o ON u.user_id = o.user_id AND o.flag = 'fA'
GROUP BY user_id

Runing example in SQLFiddle

贩梦商人 2024-07-25 06:29:31

至少有三种方法可以做到这一点:

  1. 只需使用依赖子查询。

    SELECT u.user_id, (SELECT count(user_id) FROM `order` o WHERE o.user_id = u.user_id AND o.flag = 'fA') count 
      来自“用户”u; 
      
  2. 在子查询上使用LEFT JOIN

    SELECT u.user_id, count(o.user_id) 计数 
      来自“用户”u 
      LEFT JOIN (SELECT user_id FROM `order` WHERE flag = 'fA') o USING (user_id) 
      按 u.user_id 分组; 
      
  3. 在组合条件上使用LEFT JOIN

    SELECT u.user_id, count(o.user_id) 计数 
      来自“用户”u 
      LEFT JOIN `order` o ON o.user_id = u.user_id AND o.flag = 'fA' 
      按 u.user_id 分组; 
      

我认为最快的是最后一个,但需要在足够大的数据上进行测试并查看执行计划。

SQL Fiddle

There are at least three ways to do it:

  1. Just use dependent subquery.

    SELECT u.user_id, (SELECT count(user_id) FROM `order` o WHERE o.user_id = u.user_id AND o.flag = 'fA') count
    FROM `user` u;
    
  2. Use LEFT JOIN on subquery:

    SELECT u.user_id, count(o.user_id) count
    FROM `user` u
    LEFT JOIN (SELECT user_id FROM `order` WHERE flag = 'fA') o USING (user_id)
    GROUP BY u.user_id;
    
  3. Use LEFT JOIN on combined condition:

    SELECT u.user_id, count(o.user_id) count
    FROM `user` u
    LEFT JOIN `order` o ON o.user_id = u.user_id AND o.flag = 'fA'
    GROUP BY u.user_id;
    

The fastest is last I think, but it need to be tested on large enough data and see execution plan.

SQL Fiddle

開玄 2024-07-25 06:29:31

您需要使用左外连接而不是左连接

SELECT
    u.user_id,
    COUNT(o.order_id) AS orders
FROM
    `user` AS u LEFT OUTER JOIN
    `order` AS o USING (user_id)
WHERE
    o.flag IS NULL OR
    o.flag IN ('fA')
GROUP BY
    u.user_id;

You need to use a left outer join instead of left join

SELECT
    u.user_id,
    COUNT(o.order_id) AS orders
FROM
    `user` AS u LEFT OUTER JOIN
    `order` AS o USING (user_id)
WHERE
    o.flag IS NULL OR
    o.flag IN ('fA')
GROUP BY
    u.user_id;
阳光①夏 2024-07-25 06:29:31

您可以尝试使用 CASE 语句。 我今天没有 MYSQL,所以我可以验证语法,但它应该与此接近:

  SELECT   u.user_id,
             CASE
                WHEN (SELECT COUNT (*)
                        FROM ORDER z
                       WHERE z.user_id = USER.user_id) > 0
                   THEN COUNT (*)
                ELSE 0
             END CASE AS cnt
        FROM USER
    GROUP BY USER.user_id;

You can try using a CASE statement. I don't have MYSQL here today so I can validate the syntax, but it should be close to this:

  SELECT   u.user_id,
             CASE
                WHEN (SELECT COUNT (*)
                        FROM ORDER z
                       WHERE z.user_id = USER.user_id) > 0
                   THEN COUNT (*)
                ELSE 0
             END CASE AS cnt
        FROM USER
    GROUP BY USER.user_id;
凝望流年 2024-07-25 06:29:31

该查询将起作用:

SELECT
    u.user_id,
    COUNT(o.order_id) AS orders
FROM
    `user` AS u LEFT OUTER JOIN
    (SELECT user_id, order_id FROM `order` WHERE flag IS NULL OR flag IN ('fA')) as o
    USING (user_id)
GROUP BY
    u.user_id;

This query will work:

SELECT
    u.user_id,
    COUNT(o.order_id) AS orders
FROM
    `user` AS u LEFT OUTER JOIN
    (SELECT user_id, order_id FROM `order` WHERE flag IS NULL OR flag IN ('fA')) as o
    USING (user_id)
GROUP BY
    u.user_id;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文