MySQL根据条件计算行数

时间:2018-06-09 15:56:17

标签: mysql sql

我有以下(简化)表格

users
+----+-------+
| id | name  |
+----+-------+
|  1 | alpha |
|  3 | gamma |
|  5 | five  |
|  7 | seven |
|  9 | nine  |
+----+-------+

user_relationships
+--------------+----------------+----------------------+
| from_user_id | target_user_id | relationship_type_id |
+--------------+----------------+----------------------+
|            1 |              3 |                    1 |
|            1 |              5 |                   -1 |
|            1 |              7 |                    1 |
|            1 |              9 |                    1 |
|            7 |              1 |                    1 |
+--------------+----------------+----------------------+

relationship_type_id = 1代表“跟随”

relationship_type_id = -1用于“阻止”

alpha的结果关系是:

  • alpha跟随gamma,9 [following_count = 2]
  • alpha跟随七个,七个跟随alpha [mutual_count = 1]
  • alpha阻止5 [blocking_count = 1]

伽玛的关系是:

  • alpha跟随gamma [followed_count = 1]

我需要在输出中捕获上述关系:

Output
+----+-------+-----------------+----------------+--------------+----------------+
| id | name  | following_count | followed_count | mutual_count | blocking_count |
+----+-------+-----------------+----------------+--------------+----------------+
|  1 | alpha |               2 |              0 |            1 |              1 |
|  3 | gamma |               0 |              1 |            0 |              0 |
|  5 | five  |               0 |              0 |            0 |              0 |
|  7 | seven |               0 |              0 |            1 |              0 |
|  9 | nine  |               0 |              1 |            0 |              0 |
+----+-------+-----------------+----------------+--------------+----------------+

我已经用GROUP BY,COUNT,HAVING,DISTINCT,SUM(SELECT中的SUM)等组合了几个小时,但是无法让它工作。

请寻求帮助或指导。我很乐意进一步尝试。

下面的基本MySQL查询(没有我搞乱的实验)

select 
    u.id, 
    u.name,
    r1.from_user_id, r1.target_user_id, r1.relationship_type_id,
    r2.from_user_id, r2.target_user_id, r2.relationship_type_id,
    r3.from_user_id, r3.target_user_id, r3.relationship_type_id
from users u
join user_relationships r1
    on u.id = r1.from_user_id
join user_relationships r2
    on u.id = r2.target_user_id
join user_relationships r3
    on u.id = r3.from_user_id or u.id = r3.target_user_id;

2 个答案:

答案 0 :(得分:2)

对我来说,我会尝试使用子查询解决方案。例如:

SELECT
   u.id,
   u.name,
   (
       SELECT COUNT(ur.from_user_id) 
       FROM user_relationships as ur
       WHERE 
          ur.from_user_id = u.id AND 
          NOT EXISTS (
              SELECT 1 FROM user_relationships AS ur1
              WHERE
                 ur1.target_user_id = u.id AND
                 ur1.from_user_id = ur.target_user_id
         ) AND
         ur.relationship_type_id = 1
   ) AS following_count,

   (
       SELECT COUNT(ur.target_user_id)
       FROM user_relationships AS ur
       WHERE ur.target_user_id = u.id
       AND ur.relationship_type = 1
   ) AS followed_count,

   (
      SELECT COUNT(ur.from_user_id) 
      FROM user_relationships as ur
      WHERE 
         ur.from_user_id = u.id AND 
         EXISTS (
            SELECT 1 FROM user_relation_ship AS ur1
            WHERE 
               ur1.target_user_id = u.id AND
               ur1.from_user_id = ur.target_user_id
         ) AND
         ur.relationship_type_id = 1
   ) AS mutual_count,

   (
      SELECT COUNT(ur.from_user_id) 
      FROM user_relationships as ur
      WHERE 
          ur.from_user_id = u.id AND
          ur.relationship_type_id = -1
   ) AS blocked_count

FROM users AS u

答案 1 :(得分:2)

可以使用条件聚合来实现列following_countmutual_countblocking_count。对于followed_count,您可以编写子查询。

select u.id, u.name
    , coalesce(sum(r.relationship_type_id = 1 and r1.relationship_type_id is null), 0) as following_count
    , coalesce(sum(r.relationship_type_id = 1 and r1.relationship_type_id = 1), 0) as mutual_count
    , coalesce(sum(r.relationship_type_id = -1), 0) as blocking_count
    , (
        select count(*)
        from user_relationships r2
        left join user_relationships r3 
          on r3.from_user_id = r2.target_user_id
          and r3.target_user_id = r2.from_user_id  
        where r2.target_user_id = u.id
          and r2.relationship_type_id = 1
          and r3.from_user_id is null
    ) as followed_count
from users u
left join user_relationships r on r.from_user_id = u.id
left join user_relationships r1
    on  r1.from_user_id = r.target_user_id
    and r1.target_user_id = r.from_user_id
group by u.id, u.name;

演示:http://rextester.com/WJED13044

更新1

另一种方法是首先生成全外连接,以便在一行中获得两个方向的关系。这就像是

select *
from user_relationships r1
full outer join user_relationships r2
  on  r2.from_user_id = r1.target_user_id
  and r1.from_user_id = r2.target_user_id

但是由于MySQL不支持全外连接,我们需要这样的东西:

select r.*, r1.relationship_type_id as type1, r2.relationship_type_id as type2
from (
    select from_user_id uid1, target_user_id uid2 from user_relationships
    union distinct
    select target_user_id uid1, from_user_id uid2 from user_relationships
) r
left join user_relationships r1
  on  r1.from_user_id   = r.uid1
  and r1.target_user_id = r.uid2
left join user_relationships r2
  on  r2.target_user_id = r.uid1
  and r2.from_user_id   = r.uid2;

这将返回

uid1 │ uid2 │ type1 │ type2
─────┼──────┼───────┼──────
   7 │    1 │     1 │     1
   1 │    7 │     1 │     1
   1 │    3 │     1 │  null
   1 │    5 │    -1 │  null
   1 │    9 │     1 │  null
   3 │    1 │  null │     1
   5 │    1 │  null │    -1
   9 │    1 │  null │     1

这样我们就可以在一行中的两个方向上建立关系,因此不需要followed_count列的子查询,而是可以使用条件聚合

select u.id, u.name
    , coalesce(sum(r1.relationship_type_id = 1 and r2.relationship_type_id is null), 0) as following_count
    , coalesce(sum(r2.relationship_type_id = 1 and r1.relationship_type_id is null), 0) as followed_count
    , coalesce(sum(r1.relationship_type_id = 1 and r2.relationship_type_id = 1), 0) as mutual_count
    , coalesce(sum(r1.relationship_type_id = -1), 0) as blocking_count
from users u
left join (
    select from_user_id uid1, target_user_id uid2 from user_relationships
    union distinct
    select target_user_id uid1, from_user_id uid2 from user_relationships
) r on r.uid1 = u.id
left join user_relationships r1
  on  r1.from_user_id   = r.uid1
  and r1.target_user_id = r.uid2
left join user_relationships r2
  on  r2.target_user_id = r.uid1
  and r2.from_user_id   = r.uid2
group by u.id, u.name
order by u.id;

演示:http://rextester.com/IFGLT77163

这也更灵活,因为我们现在可以轻松添加blocked_count

, coalesce(sum(r2.relationship_type_id = -1), 0) as blocked_count

如果使用MySQL 8或MariaDB 10.2,使用 CTE 可以更好地编写它:

with bdr as ( -- bidirectional relations
    select from_user_id uid1, target_user_id uid2 from user_relationships
    union distinct
    select target_user_id uid1, from_user_id uid2 from user_relationships
), rfoj as ( -- relations full outer join
    select uid1, uid2, r1.relationship_type_id type1, r2.relationship_type_id type2
    from bdr
    left join user_relationships r1
      on  r1.from_user_id   = bdr.uid1
      and r1.target_user_id = bdr.uid2
    left join user_relationships r2
      on  r2.target_user_id = bdr.uid1
      and r2.from_user_id   = bdr.uid2
)
    select u.id, u.name
        , coalesce(sum(type1 = 1 and type2 is null), 0) as following_count
        , coalesce(sum(type2 = 1 and type1 is null), 0) as followed_count
        , coalesce(sum(type1 = 1 and type2 = 1), 0) as mutual_count
        , coalesce(sum(type1 = -1), 0) as blocking_count
        , coalesce(sum(type2 = -1), 0) as blocked_count
    from users u
    left join rfoj r on r.uid1 = u.id
    group by u.id, u.name
    order by u.id

演示:https://www.db-fiddle.com/f/nEDXXkrLEj9F4dKfipzN9Q/0

更新2

在阅读你的评论并查看你在查询中尝试过的内容之后,我也有了“洞察力”,并且认为应该可以只使用两个连接而不是子查询来获得结果。

与FULL OUTER JOIN类似的结果可以通过以下方式实现:

select u.*
    , coalesce(r1.from_user_id, r2.target_user_id) as uid1
    , coalesce(r2.from_user_id, r1.target_user_id) as uid2
    , r1.relationship_type_id as type1
    , r2.relationship_type_id as type2
from users u
left join user_relationships r1 on r1.from_user_id = u.id
left join user_relationships r2
    on r2.target_user_id = u.id
    and (r2.from_user_id = r1.target_user_id or r1.from_user_id is null)

然后我们只需要像在其他查询中那样添加GROUP BY子句并执行条件聚合

select u.id, u.name
    , coalesce(sum(r1.relationship_type_id = 1 and r2.relationship_type_id is null), 0) as following_count
    , coalesce(sum(r2.relationship_type_id = 1 and r1.relationship_type_id is null), 0) as followed_count
    , coalesce(sum(r1.relationship_type_id = 1 and r2.relationship_type_id = 1), 0) as mutual_count
    , coalesce(sum(r1.relationship_type_id = -1), 0) as blocking_count
from users u
left join user_relationships r1 on r1.from_user_id = u.id
left join user_relationships r2
    on r2.target_user_id = u.id
    and (r2.from_user_id = r1.target_user_id or r1.from_user_id is null)
group by u.id, u.name
order by u.id;

演示:http://rextester.com/UAS51627

注1

OR子句中的ON条件(更新2 )可能会影响性能。这通常通过 UNION优化解决,这将导致与全外连接类似的解决方案。

注2

带有子查询( Update 1 )的LEFT JOIN也不是关于性能的最佳选择,因为没有索引可用于ON子句。最好使用INNER JOIN代替,并在应用程序中填充缺少用户(完全没有关系的用户)(如果真的需要),或者只是将它们遗漏。