Mysql multiple COUNT () from multiple tables with LEFT JOIN

I want to show the conclusion of all users.

I have 3 tables.

column

post_id(index)     user_id
1                  1
2                  3
3                  3
4                  4

photo table

photo_id(index)     user_id
1                   2
2                   4
3                   1
4                   1

desktop video

photo_id(index)     user_id
1                   4
2                   4
3                   3
4                   3

and in user table

user_id(index)     user_name
1                  mark
2                  tommy
3                  john
4                  james

in fact, it has more than 4 rows for each table.

I want to get the result as follows.

id     name      post    photo   videos
1      mark      1       2       0
2      tommy     0       1       0
3      john      2       0       2
4      james     1       1       2
5      ..        ..      ..      ..

Below is the SQL code that may work correctly, but very slowly, I would appreciate it if you could help me how it uses LEFT JOINit. Thank you

SQL

"select user.*, 
(select count(*) from post where post.userid = user.userid) postCount,
(select count(*) from photo where photo.userid = user.userid) photoCount,
(select count(*) from video where video .userid = user.userid) videoCount
from user order by user.id"

(or ORDER BY postCount, photoCount or videoCount ASC or DESC as I want)

I researched before, but no one helped me.

+4
source share
3 answers
    SELECT u.user_id, 
           u.user_name,
           COUNT(DISTINCT p.post_id) AS `postCount`,
           COUNT(DISTINCT ph.photo_id) AS `photoCount`,
           COUNT(DISTINCT v.video_id) AS `videoCount`
      FROM user u
 LEFT JOIN post p
        ON p.user_id = u.user_id
 LEFT JOIN photo ph
        ON ph.user_id = u.user_id
 LEFT JOIN video v
        ON v.user_id = u.user_id
  GROUP BY u.user_id
  ORDER BY postCount;

Live demo

+2
source

. :

select user.*, 
       (select count(*) from post where post.userid = user.userid) as postCount,
       (select count(*) from photo where photo.userid = user.userid) as photoCount,
       (select count(*) from video where video.userid = user.userid) as videoCount
from user
order by user.id;

:

post(userid)
photo(userid)
video(userid)
user(id)

, , user.id, , .

, left join - . - , - . , 125 . , 125 000 - .

+4

, , , , user_id ( - , ).

- , :

    SELECT u.user_id, 
           u.user_name, 
           ph.user_count AS 'photoCount', 
           p.user_count AS 'postCount', 
           v.user_count AS 'videoCount'
      FROM user u
INNER JOIN (  SELECT user_id,
                     COUNT(*) AS user_count 
                FROM photo 
            GROUP BY user_id
           ) ph
        ON ph.user_id=u.user_id
INNER JOIN (  SELECT user_id, 
                     COUNT(*) AS user_count 
                FROM post
            GROUP BY user_id
           ) p 
        ON p.user_id=u.user_id
INNER JOIN (  SELECT user_id, 
                     COUNT(*) AS user_count 
                FROM video 
            GROUP BY user_id
           ) v
        ON v.user_id=u.user_id

( ). ( EXPLAIN MySQL).

+1

Source: https://habr.com/ru/post/1548284/


All Articles