У меня есть 3 таблицы:
users
id device
11 SM-G955F
12 iPhone8,2
13 SM-G955F
14 LG-H812
15 SM-G955F
16 SM-G955F
17 iPhone8,2
2.
activity
user_id login_time
11 2018-05-11
12 2018-05-11
13 2018-05-11
14 2018-05-12
14 2018-05-14
15 2018-05-14
11 2018-05-14
12 2018-05-14
3
payments
user_id
15
17
11
Какой запрос мне сделать, чтобы составить рейтинг устройств на 14.05.2018 в топ-3 по количеству пользователей из активности?
Нужны три столбца:
device number_of_users number_of_users
(from activity) (from payments if there were)
Это мой запрос:
select u.device, count(distinct u.id) as number_of_users from users u inner
join activity a on a.user_id = u.id where a.login_time = '2018-04-18' group
by u.device order by number_of_users DESC limit 3;
Но я не могу отображать пользователей из платежей






используйте левое соединение для оплаты
select u.device, count(distinct u.id) as number_of_users,
count(p.user_id) as cntpay
from users u inner
join activity a on a.user_id = u.id
left join paymets p on u.id=p.user_id
where a.login_time = '2018-05-14'
group by u.device
order by number_of_users DESC limit 3
@ ЕлисейГорьков какое имя столбца в вашей таблице активности
ОШИБКА 1054 (42S22): неизвестный столбец p.id в списке полей
Думаю, вам просто нужны еще join и count(distinct):
select u.device, count(distinct u.id) as number_of_users,
count(distinct p.user_id) as number_of_payment_users
from users u inner join
activity a
on a.user_id = u.id left join
payments p
on p.user_id = u.id
where a.login_time = '2018-04-18'
group by u.device
order by number_of_users desc
limit 3;
спасибо, но у меня ОШИБКА 1054 (42S22): Неизвестный столбец 'a.id' в 'списке полей'