是否可以只对一个变量使用 DISTINCT 语句?
Is it possible to have a DISTICT statement only on one varible?
我只想在 'yearLevel' 上使用 SELECT DISTINCT 语句(因为我不希望只在 yearLevel 上有任何重复项)
image of data displayed on screen
例如,我只想要 'yearLevel' 12 条记录在 table 中,并且它是具有最低 'totalSeconds'
的记录
code
$sql = "SELECT firstName, lastName, yearLevel, totalSeconds
FROM studentInformation
JOIN record
ON studentInformation.studentId = record.student_Id
ORDER BY totalSeconds ASC
LIMIT 1 ";
是否可以这样做 -
$sql = "SELECT firstName, lastName, DISTINCT yearLevel, totalSeconds
FROM studentInformation
JOIN record
ON studentInformation.studentId = record.student_Id
ORDER BY totalSeconds ASC
LIMIT 1 ";
前提是"firstName"和"lastName"在table"studentInformation","yearLevel"和"totalSeconds"在[=24] =] "record" 这个查询应该有效。它使用相关子查询。我没有测试这个;如果它不起作用请告诉我。
SELECT a.firstName, a.lastName, b.yearLevel, b.totalSeconds
FROM studentInformation a
INNER JOIN record b ON a.studentId = b.studentId
WHERE b.totalSeconds = ( SELECT min(totalSeconds)
FROM record
WHERE yearLevel = b.yearLevel )
ORDER BY b.totalSeconds ASC
假设只有 "totalSeconds" 在 table "record" 中,这也可行。
SELECT a.firstName, a.lastName, a.yearLevel, b.totalSeconds
FROM studentInformation a
INNER JOIN record b ON a.studentId = b.studentId
WHERE b.totalSeconds = ( SELECT MIN(d.totalSeconds)
FROM studentInformation c
INNER JOIN record d ON c.studentId = d.studentId
WHERE c.yearLevel = a.yearLevel )
ORDER BY b.totalSeconds ASC
我只想在 'yearLevel' 上使用 SELECT DISTINCT 语句(因为我不希望只在 yearLevel 上有任何重复项)
image of data displayed on screen
例如,我只想要 'yearLevel' 12 条记录在 table 中,并且它是具有最低 'totalSeconds'
的记录code
$sql = "SELECT firstName, lastName, yearLevel, totalSeconds
FROM studentInformation
JOIN record
ON studentInformation.studentId = record.student_Id
ORDER BY totalSeconds ASC
LIMIT 1 ";
是否可以这样做 -
$sql = "SELECT firstName, lastName, DISTINCT yearLevel, totalSeconds
FROM studentInformation
JOIN record
ON studentInformation.studentId = record.student_Id
ORDER BY totalSeconds ASC
LIMIT 1 ";
前提是"firstName"和"lastName"在table"studentInformation","yearLevel"和"totalSeconds"在[=24] =] "record" 这个查询应该有效。它使用相关子查询。我没有测试这个;如果它不起作用请告诉我。
SELECT a.firstName, a.lastName, b.yearLevel, b.totalSeconds
FROM studentInformation a
INNER JOIN record b ON a.studentId = b.studentId
WHERE b.totalSeconds = ( SELECT min(totalSeconds)
FROM record
WHERE yearLevel = b.yearLevel )
ORDER BY b.totalSeconds ASC
假设只有 "totalSeconds" 在 table "record" 中,这也可行。
SELECT a.firstName, a.lastName, a.yearLevel, b.totalSeconds
FROM studentInformation a
INNER JOIN record b ON a.studentId = b.studentId
WHERE b.totalSeconds = ( SELECT MIN(d.totalSeconds)
FROM studentInformation c
INNER JOIN record d ON c.studentId = d.studentId
WHERE c.yearLevel = a.yearLevel )
ORDER BY b.totalSeconds ASC