我们可以在返回一行结果的 SELECT 中创建一个子查询吗?
例如,我必须使用如下查询:
SELECT columnA as aliasA, columnB as aliasB,
(SELECT columnC FROM table2
WHERE clause2) as aliasC,
(SELECT columnD FROM table2
WHERE clause2) as aliasD
FROM table1
WHERE clause1
我想改进它,因为它需要太多时间来执行。有什么方法可以创建查询,例如:
SELECT columnA as aliasA, columnB as aliasB,
(SELECT columnC, columnD FROM table2
WHERE clause2) as (aliasC, aliasD)
FROM table1
WHERE clause1
谢谢
注意:更具体地说,我举一个例子:我需要在第一季度的每个月获得学生的最佳和最差成绩。如果我加入表格,我将为同一个学生提供不止一行(每个月一个)但我需要:
SELECT firstname, lastname,
(SELECT MAX(value) FROM results WHERE results.student_id = student.id AND results.date
BETWEEN 'JAN-01' AND 'JAN-31') AS best_result_of_jan,
(SELECT MIN(value) FROM results WHERE results.student_id = student.id AND results.date
BETWEEN 'JAN-01' AND 'JAN-31') AS worst_result_of_jan,
...,
(SELECT MIN(value) FROM results WHERE results.student_id = student.id
AND results.date BETWEEN 'MAR-01' AND 'MAR-31') AS worst_result_of_mar
FROM student
WHERE student.has_maths = 1