获取相同值的id

Get id with same value

我有一个 table 如下:

studentNo  courseNo 
s001         c001  
s001         c002
s002         c001
s002         c002
s003         c001
s003         c003

请问如何才能得到与学生s001的课程一模一样的学生号?非常感谢。

编辑:我的解决方案有缺陷。等我下班回来我会修好它。现在,检查 MTO 发布的解决方案。因为它确实 return 了预期的结果 美好的一天,祝你有美好的一周。现在还是星期一早上,所以我可能是错的。但这应该可以满足您的需要

这里需要的是一个子查询,子查询会returnS001的所有课程。然后外部查询会将结果用于 return 所有拥有相同课程的学生。请注意,每个学生的结果将显示一次

SELECT DISTINCT studentNo
FROM tableName
WHERE CourseNo IN (
        SELECT CourseNo
        FROM TableName
        WHERE studentNo = 's001'
        )

Fiddle : https://www.db-fiddle.com/f/8k2Ye6deGSjLj9o9THYyys/0

Fiddle 第 2 个:https://www.db-fiddle.com/f/8k2Ye6deGSjLj9o9THYyys/1 第二个 Fiddle 不是最好的设计,但它应该可以满足您的要求

代码 Fiddle 2

SELECT DISTINCT studentNo
FROM test
WHERE studentNo NOT IN (
        SELECT studentNo
        FROM test
        WHERE CourseNo NOT IN (
                SELECT CourseNo
                FROM test
                WHERE studentNo = 's001'
                )
            AND studentNo != 's001'
        )

您没有指定您使用的数据库;但是,大多数使用 Oracle SQL Developer 作为工具的人都使用 Oracle 数据库。假设确实如此,这里有一个选项。

示例数据:

SQL> select * from study order by studentno, courseno;

STUDENTNO  COURSENO
---------- ----------
s001       c001
s001       c002
s002       c001
s002       c002
s003       c001
s003       c003
s004       c001
s004       c002
s004       c003

9 rows selected.

temp CTE 汇总 每个学生的所有课程。 listaggorder by 子句确保课程将被正确排序。

然后,在最后的 select 语句(从第 7 行开始)中,查询 returns 学生(他们不是 s001(第 9 行)将其余部分与 s001) 进行比较,后者的课程列表与学生 s001 的课程列表相同(这就是第 10 - 12 行中的子查询 returns)。

SQL> with temp as
  2    (select studentno,
  3       listagg(courseno, ';') within group (order by courseno) courses
  4     from study
  5     group by studentno
  6    )
  7  select studentno
  8  from temp
  9  where studentno <> 's001'
 10    and courses = (select courses
 11                   from temp
 12                   where studentno = 's001');

STUDENTNO
----------
s002

SQL>

同样,使用self-join:

SQL> with temp as
  2    (select studentno,
  3       listagg(courseno, ';') within group (order by courseno) courses
  4     from study
  5     group by studentno
  6    )
  7  select b.studentno
  8  from temp a join temp b on a.studentno <> b.studentno
  9                         and a.courses = b.courses
 10  where a.studentno = 's001';

STUDENTNO
----------
s002

SQL>

您想计算​​每个学生的课程数,然后将 table 连接到自己,以便为不同的学生选择相同的课程,课程总数相同,然后计算匹配的总数联接中的行等于课程总数:

WITH course_count (studentno, courseno, num_courses) AS (
  SELECT t.*,
         COUNT(courseNo) OVER (PARTITION BY studentno)
  FROM   table_name t
)
SELECT c.studentno
FROM   course_count c
       INNER JOIN course_count s
       ON (   c.courseno   =  s.courseno
          AND c.studentno  != s.studentno
          AND c.num_courses =  s.num_courses )
WHERE  s.studentno = 's001'
GROUP BY c.studentno
HAVING COUNT(c.courseno) = MAX(s.num_courses);

其中,对于(扩展的)示例数据:

CREATE TABLE table_name (studentNo, courseNo) AS
SELECT 's001', 'c001' FROM DUAL UNION ALL 
SELECT 's001', 'c002' FROM DUAL UNION ALL
SELECT 's002', 'c001' FROM DUAL UNION ALL
SELECT 's002', 'c002' FROM DUAL UNION ALL
SELECT 's003', 'c001' FROM DUAL UNION ALL
SELECT 's003', 'c003' FROM DUAL UNION ALL
SELECT 's004', 'c002' FROM DUAL UNION ALL
SELECT 's004', 'c001' FROM DUAL UNION ALL
SELECT 's004', 'c003' FROM DUAL UNION ALL
SELECT 's005', 'c001' FROM DUAL

输出:

STUDENTNO
s002

db<>fiddle here