Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何在联接查询中基于一列返回最大行数?_Mysql_Sql_Join_Count_Greatest N Per Group - Fatal编程技术网

Mysql 如何在联接查询中基于一列返回最大行数?

Mysql 如何在联接查询中基于一列返回最大行数?,mysql,sql,join,count,greatest-n-per-group,Mysql,Sql,Join,Count,Greatest N Per Group,我有一个查询,它返回一个结果集,其中包含几个表中的一些ID。如果一个数据库获取两行或多行具有相同的QuestionID,那么我只需要具有MAX(QuestionSessionID)的一行。我怎样才能做到这一点 我尝试了一系列不同的子查询变体,但没有成功。如何做到这一点 查询: SELECT AQS.QuestionSessionID, AQS.QuestionID, AQS.AnswerTextMarkerID, AQS.Correct, QG.ID AS QuestionGroupID FRO

我有一个查询,它返回一个结果集,其中包含几个表中的一些ID。如果一个数据库获取两行或多行具有相同的QuestionID,那么我只需要具有MAX(QuestionSessionID)的一行。我怎样才能做到这一点

我尝试了一系列不同的子查询变体,但没有成功。如何做到这一点

查询:

SELECT AQS.QuestionSessionID, AQS.QuestionID, AQS.AnswerTextMarkerID, AQS.Correct, QG.ID AS QuestionGroupID
FROM AnswerQuestionSession AQS
JOIN QuestionSession QS ON QS.UserID = 3 
JOIN Question Q ON AQS.QuestionID = Q.ID
JOIN QuestionGroup QG ON Q.QuestionGroupID = QG.ID
WHERE AQS.AnswerTextMarkerID IN (109,110,113,114,118,121,141,146,148,152,156,157,158,172,182,183,193,194,196,197,198,211,222,227,241,242,243,257,263,271,282,283,356,396,643,644,938,939,943,944,955,956,957,958,959,970,971,972,973,978,979,1110,1111,1112,1113,1114,1115,1116,1117,1118,1120,1121,1163,1164,1165,1166,1205,1240)
AND AQS.QuestionSessionID = QS.ID
ORDER BY AQS.QuestionID, AQS.QuestionSessionID DESC;
当前结果集:

QuestionSessionID QuestionID AnswerTextMarkerID Correct QuestionGroupID
294441            112        121                1       25
22942             112        121                0       25
22942             126        141                1       39
131489            216        257                1       102
22942             222        263                1       106
22942             227        271                1       110
294435            760        955                1       5
294435            760        956                1       5
QuestionSessionID QuestionID AnswerTextMarkerID Correct QuestionGroupID
294441            112        121                1       25
22942             126        141                1       39
131489            216        257                1       102
22942             222        263                1       106
22942             227        271                1       110
294435            760        955                1       5
294435            760        956                1       5
SELECT AQS.QuestionSessionID, AQS.QuestionID, AQS.AnswerTextMarkerID, AQS.Correct, QG.ID AS QuestionGroupID
FROM AnswerQuestionSession AQS
JOIN QuestionSession QS ON  AQS.QuestionSessionID = QS.ID 
JOIN Question Q ON AQS.QuestionID = Q.ID
JOIN QuestionGroup QG ON Q.QuestionGroupID = QG.ID
JOIN (SELECT QuestionID, MAX(QuestionSessionID) as MaxId
      FROM AnswerQuestionSession AQS2
      JOIN QuestionSession QS2 ON AQS2.QuestionSessionID = QS2.ID
      WHERE QS2.UserID = 3
      GROUP BY QuestionID) as mq ON mq.QuestionID = AQS.QuestionID
WHERE AQS.AnswerTextMarkerID IN (109,110,113,114,118,121,141,146,148,152,156,157,158,172,182,183,193,194,196,197,198,211,222,227,241,242,243,257,263,271,282,283,356,396,643,644,938,939,943,944,955,956,957,958,959,970,971,972,973,978,979,1110,1111,1112,1113,1114,1115,1116,1117,1118,1120,1121,1163,1164,1165,1166,1205,1240)
AND QS.UserID = 3 
AND AQS.QuestionSessionID = MaxId;
因此,在上面的示例中,我只需要问号为112的行中的一行(带有MAX(问号sessionid)294441的行),如下所示:

所需结果集:

QuestionSessionID QuestionID AnswerTextMarkerID Correct QuestionGroupID
294441            112        121                1       25
22942             112        121                0       25
22942             126        141                1       39
131489            216        257                1       102
22942             222        263                1       106
22942             227        271                1       110
294435            760        955                1       5
294435            760        956                1       5
QuestionSessionID QuestionID AnswerTextMarkerID Correct QuestionGroupID
294441            112        121                1       25
22942             126        141                1       39
131489            216        257                1       102
22942             222        263                1       106
22942             227        271                1       110
294435            760        955                1       5
294435            760        956                1       5
SELECT AQS.QuestionSessionID, AQS.QuestionID, AQS.AnswerTextMarkerID, AQS.Correct, QG.ID AS QuestionGroupID
FROM AnswerQuestionSession AQS
JOIN QuestionSession QS ON  AQS.QuestionSessionID = QS.ID 
JOIN Question Q ON AQS.QuestionID = Q.ID
JOIN QuestionGroup QG ON Q.QuestionGroupID = QG.ID
JOIN (SELECT QuestionID, MAX(QuestionSessionID) as MaxId
      FROM AnswerQuestionSession AQS2
      JOIN QuestionSession QS2 ON AQS2.QuestionSessionID = QS2.ID
      WHERE QS2.UserID = 3
      GROUP BY QuestionID) as mq ON mq.QuestionID = AQS.QuestionID
WHERE AQS.AnswerTextMarkerID IN (109,110,113,114,118,121,141,146,148,152,156,157,158,172,182,183,193,194,196,197,198,211,222,227,241,242,243,257,263,271,282,283,356,396,643,644,938,939,943,944,955,956,957,958,959,970,971,972,973,978,979,1110,1111,1112,1113,1114,1115,1116,1117,1118,1120,1121,1163,1164,1165,1166,1205,1240)
AND QS.UserID = 3 
AND AQS.QuestionSessionID = MaxId;
更新: 尝试按照评论员的建议添加另一个加入,但没有正确执行。它似乎只适用于具有多个相同问题ID的行:

SELECT AQS.QuestionSessionID, AQS.QuestionID, AQS.AnswerTextMarkerID, AQS.Correct, QG.ID AS QuestionGroupID, MaxId
FROM AnswerQuestionSession AQS
JOIN QuestionSession QS ON QS.UserID = 3 
JOIN Question Q ON AQS.QuestionID = Q.ID
JOIN QuestionGroup QG ON Q.QuestionGroupID = QG.ID
JOIN (SELECT QuestionID, MAX(QuestionSessionID) as MaxId
      FROM AnswerQuestionSession
      GROUP BY QuestionID) as mq ON mq.QuestionID = AQS.QuestionID
WHERE AQS.AnswerTextMarkerID IN (109,110,113,114,118,121,141,146,148,152,156,157,158,172,182,183,193,194,196,197,198,211,222,227,241,242,243,257,263,271,282,283,356,396,643,644,938,939,943,944,955,956,957,958,959,970,971,972,973,978,979,1110,1111,1112,1113,1114,1115,1116,1117,1118,1120,1121,1163,1164,1165,1166,1205,1240)
AND AQS.QuestionSessionID = QS.ID
/*AND AQS.QuestionSessionID = MaxId*/
ORDER BY AQS.QuestionID, AQS.QuestionSessionID DESC;
更新2:

QuestionSessionID QuestionID AnswerTextMarkerID Correct QuestionGroupID
294441            112        121                1       25
22942             112        121                0       25
22942             126        141                1       39
131489            216        257                1       102
22942             222        263                1       106
22942             227        271                1       110
294435            760        955                1       5
294435            760        956                1       5
QuestionSessionID QuestionID AnswerTextMarkerID Correct QuestionGroupID
294441            112        121                1       25
22942             126        141                1       39
131489            216        257                1       102
22942             222        263                1       106
22942             227        271                1       110
294435            760        955                1       5
294435            760        956                1       5
SELECT AQS.QuestionSessionID, AQS.QuestionID, AQS.AnswerTextMarkerID, AQS.Correct, QG.ID AS QuestionGroupID
FROM AnswerQuestionSession AQS
JOIN QuestionSession QS ON  AQS.QuestionSessionID = QS.ID 
JOIN Question Q ON AQS.QuestionID = Q.ID
JOIN QuestionGroup QG ON Q.QuestionGroupID = QG.ID
JOIN (SELECT QuestionID, MAX(QuestionSessionID) as MaxId
      FROM AnswerQuestionSession AQS2
      JOIN QuestionSession QS2 ON AQS2.QuestionSessionID = QS2.ID
      WHERE QS2.UserID = 3
      GROUP BY QuestionID) as mq ON mq.QuestionID = AQS.QuestionID
WHERE AQS.AnswerTextMarkerID IN (109,110,113,114,118,121,141,146,148,152,156,157,158,172,182,183,193,194,196,197,198,211,222,227,241,242,243,257,263,271,282,283,356,396,643,644,938,939,943,944,955,956,957,958,959,970,971,972,973,978,979,1110,1111,1112,1113,1114,1115,1116,1117,1118,1120,1121,1163,1164,1165,1166,1205,1240)
AND QS.UserID = 3 
AND AQS.QuestionSessionID = MaxId;
根据一位评论员的帮助,我对它进行了一个小小的修改,使它如期工作:

工作查询:

QuestionSessionID QuestionID AnswerTextMarkerID Correct QuestionGroupID
294441            112        121                1       25
22942             112        121                0       25
22942             126        141                1       39
131489            216        257                1       102
22942             222        263                1       106
22942             227        271                1       110
294435            760        955                1       5
294435            760        956                1       5
QuestionSessionID QuestionID AnswerTextMarkerID Correct QuestionGroupID
294441            112        121                1       25
22942             126        141                1       39
131489            216        257                1       102
22942             222        263                1       106
22942             227        271                1       110
294435            760        955                1       5
294435            760        956                1       5
SELECT AQS.QuestionSessionID, AQS.QuestionID, AQS.AnswerTextMarkerID, AQS.Correct, QG.ID AS QuestionGroupID
FROM AnswerQuestionSession AQS
JOIN QuestionSession QS ON  AQS.QuestionSessionID = QS.ID 
JOIN Question Q ON AQS.QuestionID = Q.ID
JOIN QuestionGroup QG ON Q.QuestionGroupID = QG.ID
JOIN (SELECT QuestionID, MAX(QuestionSessionID) as MaxId
      FROM AnswerQuestionSession AQS2
      JOIN QuestionSession QS2 ON AQS2.QuestionSessionID = QS2.ID
      WHERE QS2.UserID = 3
      GROUP BY QuestionID) as mq ON mq.QuestionID = AQS.QuestionID
WHERE AQS.AnswerTextMarkerID IN (109,110,113,114,118,121,141,146,148,152,156,157,158,172,182,183,193,194,196,197,198,211,222,227,241,242,243,257,263,271,282,283,356,396,643,644,938,939,943,944,955,956,957,958,959,970,971,972,973,978,979,1110,1111,1112,1113,1114,1115,1116,1117,1118,1120,1121,1163,1164,1165,1166,1205,1240)
AND QS.UserID = 3 
AND AQS.QuestionSessionID = MaxId;

针对
AnswerQuestionSession
表上的子查询添加联接

JOIN (SELECT QuestionID, MAX(QuestionSessionID as MaxId)
      FROM AnswerQuestionSession
      GROUP BY QuestionID) as mq ON mq. QuestionID = Aqs. QuestionID
然后在
WHERE
子句中使用它

AND Aqs.QuestionSessionID = MaxId

这是基于更新的问题,我更改了QuestionSession上的连接,并确保在WHERE子句中使用了MaxId

SELECT AQS.QuestionSessionID, AQS.QuestionID, AQS.AnswerTextMarkerID, AQS.Correct, QG.ID AS QuestionGroupID
FROM AnswerQuestionSession AQS
JOIN QuestionSession QS ON  AQS.QuestionSessionID = QS.ID
JOIN Question Q ON AQS.QuestionID = Q.ID
JOIN QuestionGroup QG ON Q.QuestionGroupID = QG.ID
JOIN (SELECT QuestionID, MAX(QuestionSessionID) as MaxId
      FROM AnswerQuestionSession
      GROUP BY QuestionID) as mq ON mq.QuestionID = AQS.QuestionID
WHERE AQS.AnswerTextMarkerID IN (109,110,113,114,118,121,141,146,148,152,156,157,158,172,182,183,193,194,196,197,198,211,222,227,241,242,243,257,263,271,282,283,356,396,643,644,938,939,943,944,955,956,957,958,959,970,971,972,973,978,979,1110,1111,1112,1113,1114,1115,1116,1117,1118,1120,1121,1163,1164,1165,1166,1205,1240)
AND QS.UserID = 3 
AND AQS.QuestionSessionID = MaxId
ORDER BY AQS.QuestionID, AQS.QuestionSessionID DESC;

@user9223839感谢您的评论。我尝试过,但没有100%奏效,我用你的建议和新的结果集更新了问题。有什么想法吗?@user9223839根据这条评论更新了问题的工作版本(稍作修改,添加了另一个join以获取maxId)。非常感谢。