SQL 查询 - 匹配 5 中至少 4 个值的条件

Posted

技术标签:

【中文标题】SQL 查询 - 匹配 5 中至少 4 个值的条件【英文标题】:SQL Query - condition to match at least 4 values from 5 【发布时间】:2019-12-22 13:41:21 【问题描述】:

我是SQL 的新手,我需要帮助编写查询(附照片图)

总结:

IG School 系统要求学生在特定的学习课程中选择 5 个学习科目,则查询结果为:

1) 具有所选主题的已可用组;

2) 至少符合 4 个所选主题的组,并且返回不匹配的主题。

表格:

Sessions -----> Like Semesters
Groups   -----> Like classes that has schedule

我已经写了一个查询,但是对于只有 4 个匹配主题的可用组没有条件,只返回匹配所有 5 个主题的组(问题),如下所示:

    SELECT Group_Subjects.Group_ID, Groups.Name as Groups_Name,  Subject_Schedule.ID as Subject_Schedule_ID,Subjects.ID Subjects_ID,
    Subjects.Name AS Subjects_Name,Sessions.Name AS Sessions_Name
    FROM   Groups 
    INNER JOIN Group_Subjects ON Groups.ID = Group_Subjects.Group_ID 
    INNER JOIN Subject_Schedule ON Group_Subjects.Subject_Schedule_ID = Subject_Schedule.ID 
    INNER JOIN Subjects ON Subject_Schedule.Subject_ID = Subjects.ID
    INNER JOIN Sessions ON dbo.Groups.Session_ID = dbo.Sessions.ID   
    where Groups.Session_ID=13 and  Subjects.ID in (16,117,125,126,127)
    order by Group_Subjects.Group_ID,Subjects_Name

照片图

【问题讨论】:

您的问题有点令人困惑。我还没有找到你说你提供的图表。提供更多信息。 我没有完全理解你的问题,但我认为是一个关系划分。请参阅右侧的“另见”栏... 【参考方案1】:

如果我理解得很好(尽管我不是 100% 肯定),您的要求包括: 1. 选择至少匹配 5 个给定“主题”中的 4 个的“组”。 2.不要包含匹配少于4个的“组”给定的5个 “主题”。

为此,您可以运行以下查询:

SELECT 
    gr.name, sub.name
FROM
    groups           gr  JOIN
    group_subject    gs  ON gs.group_id = gr.id JOIN
    subject_schedule ss  ON ss.id = gs.subject_schedule_id JOIN
    subject          sub ON sub.id = ss.subject_id  
WHERE
    EXISTS (
        SELECT 
            g.id, COUNT(*) num_subjects
        FROM
            groups           g  JOIN
            group_subject    gs  ON gs.group_id = g.id JOIN
            subject_schedule ss  ON ss.id = gs.subject_schedule_id JOIN
            subject          sub ON sub.id = ss.subject_id  
        WHERE
            sub.id IN ('A', 'B', 'C', 'D', 'E') AND
            g.id = gr.id
        GROUP BY gr.id
        HAVING num_subjects >=4
    )
ORDER BY 
    gr.name, sub.name;

除此之外,subject_schedule 似乎指的是sessions。同时groups也指sessions。 如果数据不一致,这可能会导致问题。出于这个原因,我的查询中没有包含“会话”,因为其他表包含您的用例所需的所有信息。

我已经为您的模式创建了一个示例模型来测试该查询 (mysql):

CREATE TABLE groups (
    id                   VARCHAR(5) PRIMARY KEY,
    name                 TEXT,
    session_id           VARCHAR(7) REFERENCES sessions
);
CREATE TABLE group_subject (
    id                   INTEGER PRIMARY KEY,
    group_id             VARCHAR(5) REFERENCES groups,
    subject_schedule_id  INTEGER REFERENCES subject_schedule,
    description          TEXT
);
CREATE TABLE subject_schedule (
    id                   INTEGER PRIMARY KEY,
    name                 TEXT,
    session_id           VARCHAR(7) REFERENCES sessions,
    subject_id           VARCHAR(1) REFERENCES subject
);
CREATE TABLE subject (
    id                   VARCHAR(1) PRIMARY KEY,
    name                 TEXT
);
CREATE TABLE sessions (
    id                   VARCHAR(7) PRIMARY KEY,
    name                 TEXT
);

希望对你有帮助。

【讨论】:

【参考方案2】:

您可以对所选组的结果进行分组(分组依据)并按主题的出现次数(具有 count(subject)>=4)进行过滤,如下所示:

SELECT Group_Subjects.Group_ID, Groups.Name as Groups_Name,  Subject_Schedule.ID as Subject_Schedule_ID,Subjects.ID Subjects_ID,
    Subjects.Name AS Subjects_Name,Sessions.Name AS Sessions_Name
    , **count (Subjects.ID)** 
    FROM   Groups 
    INNER JOIN Group_Subjects ON Groups.ID = Group_Subjects.Group_ID 
    INNER JOIN Subject_Schedule ON Group_Subjects.Subject_Schedule_ID = Subject_Schedule.ID 
    INNER JOIN Subjects ON Subject_Schedule.Subject_ID = Subjects.ID
    INNER JOIN Sessions ON dbo.Groups.Session_ID = dbo.Sessions.ID   
    where  Subjects.ID in (16,117,125,126,127)
    **group by** Group_Subjects.Group_ID, Groups.Name as Groups_Name,  Subject_Schedule.ID as 
    Subject_Schedule_ID,Subjects.ID Subjects_ID,
    Subjects.Name AS Subjects_Name,Sessions.Name AS Sessions_Name
    **having count (Subjects.ID) >= 4** 

【讨论】:

非常感谢,但这不会返回不匹配的主题名称或 id

以上是关于SQL 查询 - 匹配 5 中至少 4 个值的条件的主要内容,如果未能解决你的问题,请参考以下文章

数千个值的 Redshift IN 条件

SQL 计数匹配

sql server都有哪些查询优化方法

如何匹配 C# 中的 SQL 查询? [关闭]

查找字典中最大 4 个值的键

MongoTemplate 至少匹配我传递的值的查询数组