如何同时运行两个 SQL 查询?

0 mysql sql

我想同时运行两个 sql 查询,这样我就不必运行两次才能获得结果

SELECT COUNT(*) FROM attendance WHERE month =10 and grade =  4

SELECT COUNT(*) from attendance WHERE month = 10 and grade  = 4 AND userid = 24 and attendance = 'present'
Run Code Online (Sandbox Code Playgroud)

我想要两个总班级数和学生出席的班级总数。

Stu*_*Stu 5

You can combine the second criteria using conditional aggregation, this way the table is only read once.

select 
  Count(*) as TotalCount, 
  Count(case when userid = 24 and attendance = 'present' then 1 end) as StudentCount
from attendance 
where month = 10 and grade = 4;
Run Code Online (Sandbox Code Playgroud)