SQL - 如何避免将三个查询合并为一个

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL - 如何避免将三个查询合并为一个相关的知识,希望对你有一定的参考价值。

我有一个SQL脚本,用于选择已分配学生的作业。为了确定学生是否完成了他的作业,我使用了一个子查询。一旦学生完成作业,他就应该能够完成下一个作业。

我想我可以通过选择尚未完成的前1个作业(0值)来做到这一点。我可以使用第一个查询的附加查询,但随后我需要第三个查询来连接该查询。有没有办法可以实现这个前1任务的选择,其值为0,有2个查询或更少?

第一次尝试

SELECT ag.group_id, 
    ag.title, 
    ac.collection_id, 
    ag.order, 
    ac.NAME, 
    ac.isactive, 
    (SELECT top 1 iscompleted 
        FROM student_completion 
        WHERE fk_collection_id = collection_id 
        AND fk_student_id like '404') 
      AS isCompleted, 
FROM assignments AS ag 
    JOIN assignments_collection AS ac 
    ON ag.fk_collection_id = ac.collection_id 
Order BY group_id

/*
SELECT TOP 1 isCompleted
(SELECT ag.group_id, 
    ag.title, 
    ac.collection_id, 
    ag.order, 
    ac.NAME, 
    ac.isactive, 
    (SELECT top 1 iscompleted 
        FROM   student_completion 
        WHERE  fk_collection_id = collection_id 
            AND fk_student_id like '404') 
    AS isCompleted, 
FROM   assignments AS ag 
    JOIN assignments_collection AS ac 
        ON ag.fk_collection_id = ac.collection_id 
Order BY group_id)
Where isCompleted = 0 

 .........
*/

数据

+----------+--------------+---------------+-------+----------------------+----------+-------------+
| group_id |    title     | collection_id | order |         name         | isactive | isCompleted |
+----------+--------------+---------------+-------+----------------------+----------+-------------+
|        1 | Assingment_1 |             5 |     0 | Welcome to Linux     |        1 |           0 |
|        2 | Assingment_2 |             6 |     0 | Installation         |        1 |           0 |
|        3 | Assingment_3 |             9 |     1 | Intro to Bash        |        1 |           0 |
|        3 | Assingment_4 |             3 |     1 | Intro to Bash part 2 |        1 |           0 |
+----------+--------------+---------------+-------+----------------------+----------+-------------+

预期数据

+----------+--------------+---------------+-------+----------------------+----------+-------------+-----------+
| group_id |    title     | collection_id | order |         name         | isactive | isCompleted | available |
+----------+--------------+---------------+-------+----------------------+----------+-------------+-----------+
|        1 | Assingment_1 |             5 |     0 | Welcome to Linux     |        1 |           0 |         1 |
|        2 | Assingment_2 |             6 |     0 | Installation         |        1 |           0 |         0 |
|        3 | Assingment_3 |             9 |     1 | Intro to Bash        |        1 |           0 |         0 |
|        3 | Assingment_4 |             3 |     1 | Intro to Bash part 2 |        1 |           0 |         0 |
+----------+--------------+---------------+-------+----------------------+----------+-------------+-----------+

student_completion

+---------------+------------------+-------------+
| FK_studentKey | FK_collectionKey | isCompleted |
+---------------+------------------+-------------+
|           404 |                5 |           1 |
+---------------+------------------+-------------+
答案

你的问题非常缺乏解释。我在这里黑暗中拍摄。这样做你想要的吗?

select d.*
    , available = sc.isCompleted
from Data d
left join student_completion sc on sc.FK_colletionKey = d.collection_id

以上是关于SQL - 如何避免将三个查询合并为一个的主要内容,如果未能解决你的问题,请参考以下文章

SQL中,如何将三个表中同一字段中的内容汇总到一个字段中?

在这种情况下如何避免嵌套 SQL 查询?

将嵌套查询转换为 sql 视图

mybatis查询结果如何合并为列表

在 SQL 中,如何在不使用大小写的情况下将两个查询合并为一个?

将三个使用 1:* 关系的查询合并为一个查询