sql select查询以获取特定用户的帖子,关注者和关注者的数量

时间:2019-02-14 07:28:16

标签: sql select mariadb

我有一个名为sdb的数据库,其中有3个表userspostsusers_follow

以下所有3个表的创建表查询

-- users table

CREATE TABLE `users` (
 `user_id` int(11) NOT NULL AUTO_INCREMENT,
 `uname` varchar(25) NOT NULL,
 `email` varchar(35) NOT NULL,
 `password` varchar(255) NOT NULL,
 `user_status` enum('active','inactive','remove') DEFAULT 'active',
 `signup_at` datetime DEFAULT CURRENT_TIMESTAMP,
 PRIMARY KEY (`user_id`)
) ENGINE=InnoDB;

users table data

-- posts table

CREATE TABLE `posts` (
 `post_id` int(11) NOT NULL AUTO_INCREMENT,
 `title` tinytext NOT NULL,
 `content` text NOT NULL,
 `posted_at` datetime DEFAULT CURRENT_TIMESTAMP,
 `user_id` int(11) DEFAULT NULL,
 PRIMARY KEY (`post_id`),
 KEY `posts_fk1` (`user_id`),
 CONSTRAINT `posts_fk1` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB;

posts table data

-- users_follow table

CREATE TABLE `users_follow` (
 `follow_id` int(11) NOT NULL AUTO_INCREMENT,
 `follower_id` int(11) DEFAULT NULL,
 `following_id` int(11) NOT NULL,
 `follow_status` enum('active','blocked') DEFAULT 'active',
 PRIMARY KEY (`follow_id`),
 KEY `users_follow_fk1` (`follower_id`),
 KEY `users_follow_fk2` (`following_id`),
 CONSTRAINT `users_follow_fk1` FOREIGN KEY (`follower_id`) REFERENCES `users` (`user_id`) ON DELETE CASCADE ON UPDATE CASCADE,
 CONSTRAINT `users_follow_fk2` FOREIGN KEY (`following_id`) REFERENCES `users` (`user_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB;

users_follow table data

我尝试以下查询(对于user_id = 1的用户)

SELECT a.user_id,
   SUM(CASE WHEN f.following_id = 1 THEN 1 ELSE 0 END) AS count_followers,
   SUM(CASE WHEN f.follower_id = 1 THEN 1 ELSE 0 END) AS count_followings,
   SUM(CASE WHEN p.user_id = 1 THEN 1 ELSE 0 END) AS count_posts
FROM users_follow f
RIGHT JOIN users a
ON f.following_id = a.user_id
RIGHT JOIN users b
ON f.follower_id = b.user_id
LEFT JOIN posts p
ON p.user_id = a.user_id
where a.user_id = 1;

以上查询返回count_postscount_followerscount_following = 0的相同计数

输出: output

2 个答案:

答案 0 :(得分:1)

我想您可能需要遵循这些原则

select a.user_id,
       (
          select count(*)
          from posts p
          where a.user_id = p.user_id
       ) count_posts,
       (
          select count(*)
          from users_follow f
          where f.follower_id = a.user_id
       ) count_followings,
       (
          select count(*)
          from users_follow f
          where f.following_id = a.user_id
       ) count_followers
from users a

如果您想使用GROUP BY和外部联接来编写它,则绝对需要使其成为内联子查询。因此,像这样

select a.user_id,
       p.count_posts,
       f1.count_followings,
       f2.count_followers
from users a
left join (
  select a.user_id, count(*)
  from posts p
  group by a.user_id
) p on p.user_id = a.user_id
left join 
(
      select f.follower_id, count(*) count_followings
      from users_follow f
      group by  f.follower_id
) f2
left join f2.follower_id = a.user_id
(
      select f.following_id, count(*) count_followers
      from users_follow f
      group by  f.following_id
) f2 on f2.following_id = a.user_id

答案 1 :(得分:0)

implementation 'com.google.firebase:firebase-core:16.0.0' implementation 'com.google.firebase:firebase-auth:16.0.1' implementation 'com.google.firebase:firebase-messaging:17.0.0' 表的性能可以提高:

user_follow