一个查询中的多个查询在哪里(multiple query in one query where)

编程入门 行业动态 更新时间:2024-10-26 04:20:07
一个查询中的多个查询在哪里(multiple query in one query where)

我写了这个查询来查找2个表中的最高分(选择项: StudentId和Score ),现在我想从其他表中选择一些其他数据:( StudentName , StudentImage ,...),这些表必须使用StudentId过滤项目

我的查询返回:

StudentId HighScoreUser -1 250 -2 100 -3 90 -4 80 -5 40

为了在网格中显示数据,我需要学生姓名,...所以我必须使用StudentId来查找特定用户的信息:

CREATE PROCEDURE SelectTopYear AS SELECT TOP 5 StudentId, ISNULL(SUM(Score),0) As HighScoreUser FROM (SELECT StudentId, Score FROM tbl_ActPoint UNION ALL SELECT StudentId, Score FROM tbl_EvaPoint ) as T GROUP BY StudentId ORDER BY HighScoreUser DESC RETURN 0

I wrote this query for finding the highest score from 2 tables (Selected Item: StudentId and Score), and now I want to select some other data: (StudentName, StudentImage, ...) from other tables that must filter items using StudentId

my query returns:

StudentId HighScoreUser -1 250 -2 100 -3 90 -4 80 -5 40

For showing data in a grid, I need the Student Name, ... so I must use StudentId to find the info for the specific user:

CREATE PROCEDURE SelectTopYear AS SELECT TOP 5 StudentId, ISNULL(SUM(Score),0) As HighScoreUser FROM (SELECT StudentId, Score FROM tbl_ActPoint UNION ALL SELECT StudentId, Score FROM tbl_EvaPoint ) as T GROUP BY StudentId ORDER BY HighScoreUser DESC RETURN 0

最满意答案

您可以使用CTE(或子查询)和JOIN :

WITH s as ( SELECT TOP 5 StudentId, ISNULL(SUM(Score),0) As HighScoreUser FROM (SELECT StudentId, Score FROM tbl_ActPoint UNION ALL SELECT StudentId, Score FROM tbl_EvaPoint ) s GROUP BY StudentId ORDER BY HighScoreUser DESC ) SELECT . . . FROM s JOIN othertable ot ON s.StudentId = ot.StudentId;

填写适当的列名和表名。

You can use a CTE (or subquery) and JOIN:

WITH s as ( SELECT TOP 5 StudentId, ISNULL(SUM(Score),0) As HighScoreUser FROM (SELECT StudentId, Score FROM tbl_ActPoint UNION ALL SELECT StudentId, Score FROM tbl_EvaPoint ) s GROUP BY StudentId ORDER BY HighScoreUser DESC ) SELECT . . . FROM s JOIN othertable ot ON s.StudentId = ot.StudentId;

Fill in the appropriate column names and table names.

更多推荐

本文发布于:2023-07-25 10:39:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1259994.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   multiple   query

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!