如何在同一个表上执行多个 select 语句?

发布于 2024-10-07 01:11:41 字数 997 浏览 1 评论 0原文

我有一张表存储一些学生作业信息。我需要根据学生 ID 和季度 ID 选择时间。这就是我所拥有的:

SELECT
(SELECT hours FROM clinicalStudents WHERE quarterID='201101' and studentID='$studentID') as q1,             
(SELECT hours FROM clinicalStudents WHERE quarterID='201102' and studentID='$studentID') as q2,             
(SELECT hours FROM clinicalStudents WHERE quarterID='201103' and studentID='$studentID') as q3,             
(SELECT hours FROM clinicalStudents WHERE quarterID='201104' and studentID='$studentID') as q4

它只给了我一些数字,但不是全部。我在服务器管理器中运行此命令(减去 WHERE 子句)并收到错误:

“子查询返回超过 1 个值。当子查询遵循 =、!=、<、<=、>、> 时,这是不允许的;= 或当子查询用作表达式时"

任何帮助都会很棒。谢谢!

编辑:

$studentID 是在 while 循环中生成的,因此我在转到下一个学生之前使用该学生的小时数。我获取每个季度一名学生的所有时间,添加它们(这必须在 sql 之外完成),将结果存储在变量中,然后移动到下一个学生。当我得到 1 个季度时,这很有效,但我在获取所有季度时遇到问题。

编辑第二轮: 我想是以一种相当懒惰的方式做到的:

我只是为特定学生选择了所有小时和季度 ID。然后运行了一段时间(odbc_fetch_row())。如果是 201101,我将其添加到 $q1 堆,201102 添加到 $q2 堆,依此类推。处理速度有点慢,但对我正在做的事情来说并不是一个大问题。

I have a table that stores some student work info. I need to select the hours based on the studentID and the quarter ID. Here is what I have:

SELECT
(SELECT hours FROM clinicalStudents WHERE quarterID='201101' and studentID='$studentID') as q1,             
(SELECT hours FROM clinicalStudents WHERE quarterID='201102' and studentID='$studentID') as q2,             
(SELECT hours FROM clinicalStudents WHERE quarterID='201103' and studentID='$studentID') as q3,             
(SELECT hours FROM clinicalStudents WHERE quarterID='201104' and studentID='$studentID') as q4

It's only giving me some numbers but not all of them. I ran this (minus the WHERE clause) in my server manager and received an error:

"Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <=, >, >= or when the subquery is used as an expression"

Any assistance would be great. Thanks!

EDIT:

The $studentID is generated in a while loop so I'm using the hours on that student before I move to the next one. I'm getting all the hours for one student in each quarter, adding them (this has to be done outside of the sql), storing results in a variable then moving to the next student. This works perfect when I get 1 quarter but i'm having an issue getting all the quarters.

EDIT Round 2:
Did it in a rather lazy way I suppose:

I just selected all the hours and quarterID's for a specific student. Then ran a while(odbc_fetch_row()). If it was a 201101 I added it to the $q1 pile, 201102 added to the $q2 pile, and so on. Processing is a little slower but not a big issue with what I'm doing.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

习惯成性 2024-10-14 01:11:41

尝试在查询中使用 SELECT TOP 1LIMIT 1,具体取决于您运行的 SQL。

编辑
另外,你为什么要努力实现?这看起来很笨拙,并且根据您的预期目的,可能有更好的方法可用。

trying use a SELECT TOP 1 or a LIMIT 1 in the query, depending which sql you are running.

EDIT
Also, why are you trying to accomplish? This seems clunky and, depending your intended purpose, there is probably a better way available.

记忆消瘦 2024-10-14 01:11:41

我不确定你的目标是什么......

也许你真正想要的是:

select quarterID, sum(hours) 
from clinicalStudents 
where studentID='$studentID' 
group by 1

I'm not sure what's your goal...

Maybe what you really want is:

select quarterID, sum(hours) 
from clinicalStudents 
where studentID='$studentID' 
group by 1
千寻… 2024-10-14 01:11:41
SELECT 
hours, SUBSTR(quarterId, 4, 2)
FROM 
clinicalStudents 
WHERE 
quarterID IN ('201101', '201102', '201103', '201104') and studentID='$studentID'

根据您使用的数据库,您必须更改函数 SUBSTR

SELECT 
hours, SUBSTR(quarterId, 4, 2)
FROM 
clinicalStudents 
WHERE 
quarterID IN ('201101', '201102', '201103', '201104') and studentID='$studentID'

Depending on what database you are using, you will have to change the function SUBSTR

GRAY°灰色天空 2024-10-14 01:11:41

循环编辑

不要使用循环来执行 SUM 运算。相反,使用 SUM 聚合运算

允许您将每个季度投影到每个列。

   SELECT
 cs.studentid , q1.hours Q1, q2.hours Q2, q3.hours Q3, q4.hours Q4
    FROM 
    clinicalStudents cs
    (SELECT SUM(hours) hours , studentID 
     FROM clinicalStudents 
     WHERE quarterID='201101' and studentID='$studentID' 
     GROUP BY studentID ) q1
    LEFT join on cs.studentID = q1.clinicalStudents 
    (SELECT SUM(hours) hours , studentID  
     FROM clinicalStudents WHERE quarterID='201102' and studentID='$studentID' 
    GROUP BY studentID )  q2
    LEFT join on cs.studentID = q2.clinicalStudents              
    (SELECT SUM(hours) hours  , studentID 
     FROM clinicalStudents 
     WHERE quarterID='201103' and studentID='$studentID'
     GROUP BY studentID ) q3             
    LEFT join on cs.studentID = q3.clinicalStudents              
    (SELECT SUM(hours) hours , studentID 
     FROM clinicalStudents 
     WHERE quarterID='201104' and studentID='$studentID' GROUP BY studentID ) q4
    LEFT join on cs.studentID = q4.clinicalStudents          
    WHERE cs.studentID='$studentID'

EDIT on loop.

Don't use a loop to do a SUM operation. Instead use the SUM aggregation operation

The allows you to project each quarter to each column.

   SELECT
 cs.studentid , q1.hours Q1, q2.hours Q2, q3.hours Q3, q4.hours Q4
    FROM 
    clinicalStudents cs
    (SELECT SUM(hours) hours , studentID 
     FROM clinicalStudents 
     WHERE quarterID='201101' and studentID='$studentID' 
     GROUP BY studentID ) q1
    LEFT join on cs.studentID = q1.clinicalStudents 
    (SELECT SUM(hours) hours , studentID  
     FROM clinicalStudents WHERE quarterID='201102' and studentID='$studentID' 
    GROUP BY studentID )  q2
    LEFT join on cs.studentID = q2.clinicalStudents              
    (SELECT SUM(hours) hours  , studentID 
     FROM clinicalStudents 
     WHERE quarterID='201103' and studentID='$studentID'
     GROUP BY studentID ) q3             
    LEFT join on cs.studentID = q3.clinicalStudents              
    (SELECT SUM(hours) hours , studentID 
     FROM clinicalStudents 
     WHERE quarterID='201104' and studentID='$studentID' GROUP BY studentID ) q4
    LEFT join on cs.studentID = q4.clinicalStudents          
    WHERE cs.studentID='$studentID'
橘虞初梦 2024-10-14 01:11:41

试试这个。

select studentID
      ,sum(case when quarterID = '201101' then hours end) as q1
      ,sum(case when quarterID = '201102' then hours end) as q2
      ,sum(case when quarterID = '201103' then hours end) as q3
      ,sum(case when quarterID = '201104' then hours end) as q4
  from clinicalStudents
 where quarterID in('201101', '201102', '201103', '201104')
 group by studentID;

Try this.

select studentID
      ,sum(case when quarterID = '201101' then hours end) as q1
      ,sum(case when quarterID = '201102' then hours end) as q2
      ,sum(case when quarterID = '201103' then hours end) as q3
      ,sum(case when quarterID = '201104' then hours end) as q4
  from clinicalStudents
 where quarterID in('201101', '201102', '201103', '201104')
 group by studentID;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文