-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1623-AllValidTripletsThatCanRepresentACountry.sql
More file actions
126 lines (119 loc) · 4.62 KB
/
1623-AllValidTripletsThatCanRepresentACountry.sql
File metadata and controls
126 lines (119 loc) · 4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
-- 1623. All Valid Triplets That Can Represent a Country
-- Table: SchoolA
-- +---------------+---------+
-- | Column Name | Type |
-- +---------------+---------+
-- | student_id | int |
-- | student_name | varchar |
-- +---------------+---------+
-- student_id is the column with unique values for this table.
-- Each row of this table contains the name and the id of a student in school A.
-- All student_name are distinct.
-- Table: SchoolB
-- +---------------+---------+
-- | Column Name | Type |
-- +---------------+---------+
-- | student_id | int |
-- | student_name | varchar |
-- +---------------+---------+
-- student_id is the column with unique values for this table.
-- Each row of this table contains the name and the id of a student in school B.
-- All student_name are distinct.
-- Table: SchoolC
-- +---------------+---------+
-- | Column Name | Type |
-- +---------------+---------+
-- | student_id | int |
-- | student_name | varchar |
-- +---------------+---------+
-- student_id is the column with unique values for this table.
-- Each row of this table contains the name and the id of a student in school C.
-- All student_name are distinct.
-- There is a country with three schools, where each student is enrolled in exactly one school.
-- The country is joining a competition and wants to select one student from each school to represent the country such that:
-- member_A is selected from SchoolA,
-- member_B is selected from SchoolB,
-- member_C is selected from SchoolC, and
-- The selected students' names and IDs are pairwise distinct (i.e. no two students share the same name, and no two students share the same ID).
-- Write a solution to find all the possible triplets representing the country under the given constraints.
-- Return the result table in any order.
-- The result format is in the following example.
-- Example 1:
-- Input:
-- SchoolA table:
-- +------------+--------------+
-- | student_id | student_name |
-- +------------+--------------+
-- | 1 | Alice |
-- | 2 | Bob |
-- +------------+--------------+
-- SchoolB table:
-- +------------+--------------+
-- | student_id | student_name |
-- +------------+--------------+
-- | 3 | Tom |
-- +------------+--------------+
-- SchoolC table:
-- +------------+--------------+
-- | student_id | student_name |
-- +------------+--------------+
-- | 3 | Tom |
-- | 2 | Jerry |
-- | 10 | Alice |
-- +------------+--------------+
-- Output:
-- +----------+----------+----------+
-- | member_A | member_B | member_C |
-- +----------+----------+----------+
-- | Alice | Tom | Jerry |
-- | Bob | Tom | Alice |
-- +----------+----------+----------+
-- Explanation:
-- Let us see all the possible triplets.
-- - (Alice, Tom, Tom) --> Rejected because member_B and member_C have the same name and the same ID.
-- - (Alice, Tom, Jerry) --> Valid triplet.
-- - (Alice, Tom, Alice) --> Rejected because member_A and member_C have the same name.
-- - (Bob, Tom, Tom) --> Rejected because member_B and member_C have the same name and the same ID.
-- - (Bob, Tom, Jerry) --> Rejected because member_A and member_C have the same ID.
-- - (Bob, Tom, Alice) --> Valid triplet.
-- Create table If Not Exists SchoolA (student_id int, student_name varchar(20))
-- Create table If Not Exists SchoolB (student_id int, student_name varchar(20))
-- Create table If Not Exists SchoolC (student_id int, student_name varchar(20))
-- Truncate table SchoolA
-- insert into SchoolA (student_id, student_name) values ('1', 'Alice')
-- insert into SchoolA (student_id, student_name) values ('2', 'Bob')
-- Truncate table SchoolB
-- insert into SchoolB (student_id, student_name) values ('3', 'Tom')
-- Truncate table SchoolC
-- insert into SchoolC (student_id, student_name) values ('3', 'Tom')
-- insert into SchoolC (student_id, student_name) values ('2', 'Jerry')
-- insert into SchoolC (student_id, student_name) values ('10', 'Alice')
SELECT
a.name AS member_A,
b.name AS member_B,
c.name AS member_C
FROM
(
SELECT
student_id AS id,
student_name AS name
FROM
SchoolA
) AS a,
(
SELECT
student_id AS id,
student_name AS name
FROM
SchoolB
) AS b,
(
SELECT
student_id AS id,
student_name AS name
FROM
SchoolC
) AS c
WHERE
a.id != b.id AND b.id != c.id AND a.id != c.id AND -- ID 不能相同
a.name != b.name AND b.name != c.name AND a.name != c.name -- 名字不能相同