返回介绍

solution / 1600-1699 / 1623.All Valid Triplets That Can Represent a Country / README_EN

发布于 2024-06-17 01:03:16 字数 4411 浏览 0 评论 0 收藏 0

1623. All Valid Triplets That Can Represent a Country

中文文档

Description

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.

Solutions

Solution 1

# Write your MySQL query statement below
SELECT
  a.student_name AS member_A,
  b.student_name AS member_B,
  c.student_name AS member_C
FROM
  SchoolA AS a,
  SchoolB AS b,
  SchoolC AS c
WHERE
  a.student_name != b.student_name
  AND a.student_name != c.student_name
  AND b.student_name != c.student_name
  AND a.student_id != b.student_id
  AND a.student_id != c.student_id
  AND b.student_id != c.student_id;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文