Количество стран других пользователей - PullRequest
1 голос
/ 02 августа 2020

У меня есть три таблицы trip_country_sets , users и trip .

CREATE TABLE trip_country_sets (
tc_set_id varchar PRIMARY KEY,
trip_id varchar,
user_id varchar,
country varchar
);

CREATE TABLE users (
user_id varchar PRIMARY KEY,
name_t varchar
);

CREATE TABLE trip (
trip_id varchar PRIMARY KEY,
owner_id varchar,
title varchar
);insert into trip_country_sets values('tc1', 'trip1', 'usr1', 'counrty_test1');
insert into trip_country_sets values('tc2', 'trip2', 'usr2', 'counrty_test1');
insert into trip_country_sets values('tc3', 'trip3', 'usr3', 'counrty_test2');
insert into trip_country_sets values('tc4', 'trip4', 'usr1', 'counrty_test2');
insert into trip_country_sets values('tc5', 'trip5', 'usr2', 'counrty_test3');
insert into trip_country_sets values('tc6', 'trip6', 'usr3', 'counrty_test3');
insert into trip_country_sets values('tc7', 'trip3', 'usr1', 'counrty_test4');
insert into trip_country_sets values('tc8', 'trip4', 'usr2', 'counrty_test4');
insert into trip_country_sets values('tc9', 'trip1', 'usr3', 'counrty_test5');
insert into trip_country_sets values('tc10', 'trip2', 'usr1', 'counrty_test5');
insert into trip_country_sets values('tc11', 'trip1', 'usr2', 'counrty_test1');
insert into trip_country_sets values('tc12', 'trip1', 'usr3', 'counrty_test1');
insert into trip_country_sets values('tc13', 'trip4', 'usr2', 'counrty_test3');
insert into trip_country_sets values('tc14', 'trip5', 'usr1', 'counrty_test5');
insert into trip_country_sets values('tc15', 'trip6', 'usr2', 'counrty_test6');
insert into trip_country_sets values('tc16', 'trip2', 'usr3', 'counrty_test3');
insert into trip_country_sets values('tc17', 'trip1', 'usr2', 'counrty_test5');

insert into users values('usr1', 'name1');
insert into users values('usr2', 'name2');
insert into users values('usr3', 'name3');

insert into trip values('trip1', 'usr1', 'title1');
insert into trip values('trip2', 'usr2', 'title2');
insert into trip values('trip3', 'usr3', 'title3');
insert into trip values('trip4', 'usr1', 'title4');
insert into trip values('trip5', 'usr5', 'title5');
insert into trip values('trip6', 'usr1', 'title6');
insert into trip values('trip7', 'usr4', 'title4');
insert into trip values('trip8', 'usr1', 'title5');
insert into trip values('trip9', 'usr6', 'title6');

Как сделать запрос, который найдет все стран пользователя, количество стран , не принадлежащих пользователю.

select
    trip_country_sets.country,
    count(distinct trip_country_sets.tc_set_id) as country_count
from trip_country_sets
    LEFT OUTER join trip on trip_country_sets.trip_id = trip.trip_id
    LEFT OUTER JOIN users on trip_country_sets.user_id = users.user_id
where
    trip.owner_id = 'usr1'
    and trip_country_sets.user_id <> 'usr1'
group by trip_country_sets.country, trip_country_sets.tc_set_id;

Результат моего запроса влияет только на страны и подсчитывают страны из таблицы trip_country_sets . WHERE значения участвуют в определении запроса trip.owner_id = 'usr1' и trip_country_sets.user_id <> 'usr1' Я хочу следующее результат:

---------------------------------
|    country    | country_count |
---------------------------------
| counrty_test1 |       2       |
| counrty_test3 |       2       |
| counrty_test6 |       1       |
| counrty_test5 |       2       |
| counrty_test4 |       1       |

Пример работы: https://rextester.com/live/GOKNM17080

Ответы [ 2 ]

1 голос
/ 02 августа 2020
• 1000
0 голосов
/ 02 августа 2020

Я нашел ответ. Может, я не совсем точно объяснил вопрос. Вот мой поисковый запрос, это правильный ответ:

select
    trip_country_sets.country,
    count(distinct trip_country_sets.tc_set_id) as country_count
from trip_country_sets
    LEFT OUTER join trip on trip_country_sets.trip_id = trip.trip_id
    LEFT OUTER JOIN users on trip_country_sets.user_id = users.user_id
where
    trip.owner_id = 'usr1'
    and trip_country_sets.user_id <> 'usr1'
group by trip_country_sets.country;

Этот запрос возвращает желаемый результат:

---------------------------------
|    country    | country_count |
---------------------------------
| counrty_test1 |       2       |
| counrty_test3 |       2       |
| counrty_test6 |       1       |
| counrty_test5 |       2       |
| counrty_test4 |       1       |
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...