I have three tables: users, items, and boards.
create table users (
id serial primary key,
name text
);
create table items (
id serial primary key,
user_id int,
title text
);
create table boards (
id serial primary key,
user_id int,
title text
);n
I want to select all the users with the number of items and boards each user has:
select
users.*,
count(items) as item_count,
count(boards) as board_count
from users
left join items on items.user_id = users.id
left join boards on boards.user_id = users.id
group by users.id;
Unfortunately, this does not give me the right counts. The counts are way higher than they should be. What is happening, why, and how can I fix this?