我有一個表比較。如果我運行選擇兩列總和的最大值
SELECT comparisonID,stu1Vers,stu2Vers,stu1,stu2
from comparisons
WHERE stu1!=stu2 and assignmentid=9;
我得到的是這樣的:
+--------------+----------+----------+------+------+
| comparisonID | stu1Vers | stu2Vers | stu1 | stu2 |
+--------------+----------+----------+------+------+
| 287 | 12 | 2 | 1 | 6 |
| 286 | 12 | 1 | 1 | 6 |
| 276 | 11 | 2 | 1 | 6 |
| 275 | 11 | 1 | 1 | 6 |
| 266 | 10 | 2 | 1 | 6 |
| 265 | 10 | 1 | 1 | 6 |
| 257 | 9 | 2 | 1 | 6 |
| 256 | 9 | 1 | 1 | 6 |
...
| 391 | 19 | 1 | 1 | 6 |
| 392 | 19 | 2 | 1 | 6 |
+--------------+----------+----------+------+------+
我想選擇整行,其中stu1Vers + stu2Vers是最大的。我一直試圖沿着
select c.comparisonid,c.stu1vers,c.stu2vers,max(totvers)
from comparisons as c join
(select comparisonid, stu1vers+stu2vers as totvers
from comparisons where stu1!=stu2 group by comparisonid) as cm
on c.comparisonid = cm.comparisonid and c.stu1vers+c.stu2vers = cm.totvers;
線的東西,但它返回的東西,而隨機分類:
+--------------+----------+----------+--------------+
| comparisonid | stu1vers | stu2vers | max(totvers) |
+--------------+----------+----------+--------------+
| 220 | 1 | 1 | 21 |
+--------------+----------+----------+--------------+
我試圖讓在第一個錶行392。
如果存在具有相同最大值的多行,會發生什麼?全部退回,還是隻有一個?如果後者是哪一個? –
表中實際上還有兩列,分別是stu1和stu2的id。所以歸還所有這些,但由stu1或stu2分組)。對於任何學生任何作業,max(stu1vers + stu2vers)保證是唯一的。 –