如何在同一个表上执行多个 select 语句?
我有一张表存储一些学生作业信息。我需要根据学生 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试在查询中使用
SELECT TOP 1
或LIMIT 1
,具体取决于您运行的 SQL。编辑
另外,你为什么要努力实现?这看起来很笨拙,并且根据您的预期目的,可能有更好的方法可用。
trying use a
SELECT TOP 1
or aLIMIT 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.
我不确定你的目标是什么......
也许你真正想要的是:
I'm not sure what's your goal...
Maybe what you really want is:
根据您使用的数据库,您必须更改函数 SUBSTR
Depending on what database you are using, you will have to change the function SUBSTR
循环编辑。
不要使用循环来执行 SUM 运算。相反,使用 SUM 聚合运算
允许您将每个季度投影到每个列。
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.
试试这个。
Try this.