查询以选择忽略重复的记录

时间:2017-03-11 15:00:40

标签: mysql select duplicates unique

我需要返回一份具有相应任务结果的学生列表,但是,学生可以重复该任务,这意味着有重复。我想得到第一个结果,但我想得到班上所有学生的第一个结果。

我当前的查询返回包括重复项在内的所有结果:

SELECT student_id, attempt_id, task_score
FROM Attempt JOIN SetPaper ON Attempt.paper_id = SetPaper.paper_id
WHERE SetPaper.task_id = {X} AND class_id = {Y} AND task_status = "complete";

其中' X'和' Y'是预定的变量。

目前将返回:

+------------+------------+------------+
| student_id | attempt_id | task_score |
+------------+------------+------------+
|    X0000   |      1     |     70     |
|    X0001   |      2     |     40     |
|    X0001   |      3     |     50     |
+------------+------------+------------+

这是因为在这种情况下学生' X0001'尝试过两次完成任务。但是,我希望它的格式为:

+------------+------------+------------+
| student_id | attempt_id | task_score |
+------------+------------+------------+
|    X0000   |      1     |     70     |
|    X0001   |      2     |     40     |
+------------+------------+------------+

因此它只能获得他们的第一次尝试结果。

我尝试过使用DISTINCT(student_id),但如果我想返回的只是学生ID,那么仍会得到其他结果。

谢谢,

瑞恩。

1 个答案:

答案 0 :(得分:1)

快速回答(TL; DR)

  • DeveloperAndreaCook希望执行一个JOIN查询,在表上返回不同的结果。
  • 可以使用MySQL GROUP BY
  • 完成

详细答案

上下文

  • MySQL 5.x
  • SELECT JOIN Query
  • 的明显结果

问题

  • 场景:开发人员希望执行返回不同结果的JOIN查询

解决方案

  • 使用GROUP BY Expression

样本数据

qqperson
  +----+---------+
  | id | fname   |
  +----+---------+
  |  1 | alice   |
  |  2 | bobby   |
  |  3 | charlie |
  |  4 | danny   |
  |  5 | eddie   |
  |  6 | freddy  |
  +----+---------+

qqtask +----+------------+-----------+-------------+ | id | taskname | taskscore | qqperson_id | +----+------------+-----------+-------------+ | 1 | action-a | 40 | 1 | | 2 | action-aa | 50 | 1 | | 3 | action-b | 40 | 2 | | 4 | action-c | 50 | 3 | | 5 | action-d | 50 | 4 | | 6 | action-aaa | 60 | 1 | +----+------------+-----------+-------------+

Example00

  • example00(查询)包含不需要的重复项的示例查询

    SELECT
      'x' AS `x`
      ,`qqperson`.`id` AS `person_id`
      ,`qqperson`.`fname` AS `fname`
      ,`qqtask`.`qqperson_id` AS `qqperson_id`
      ,`qqtask`.`taskscore` AS `taskscore`
      ,`qqtask`.`id` AS `task_id`
    FROM
      (
        `qqperson`
        JOIN `qqtask` ON (
          (
            `qqtask`.`qqperson_id` = `qqperson`.`id`
          )
        )
      )
    
  • example00(结果)

    x    | person_id | fname   | qqperson_id | taskscore | task_id
    x    | 1         | alice   | 1           | 40        | 1      
    x    | 1         | alice   | 1           | 50        | 2      
    x    | 2         | bobby   | 2           | 40        | 3      
    x    | 3         | charlie | 3           | 50        | 4      
    x    | 4         | danny   | 4           | 50        | 5      
    x    | 1         | alice   | 1           | 60        | 6      
    

Example01

  • example01(查询)忽略ORDER BYGROUP BY的重复项

    SELECT
      'x' AS `x`
      ,`qqperson`.`id` AS `person_id`
      ,`qqperson`.`fname` AS `fname`
      ,`qqtask`.`qqperson_id` AS `qqperson_id`
      ,`qqtask`.`taskscore` AS `taskscore`
      ,`qqtask`.`id` AS `task_id`
    FROM
      (
        `qqperson`
        JOIN `qqtask` ON (
          (
            `qqtask`.`qqperson_id` = `qqperson`.`id`
          )
        )
      )
    
    GROUP BY
      `qqperson`.`id`
    
    ORDER BY
      `qqperson`.`id`      
    
    
  • example01(结果)

    x    | person_id | fname   | qqperson_id | taskscore | task_id
    x    | 1         | alice   | 1           | 40        | 1      
    x    | 2         | bobby   | 2           | 40        | 3      
    x    | 3         | charlie | 3           | 50        | 4      
    x    | 4         | danny   | 4           | 50        | 5      
    

陷阱

  • ORDER BY子句将影响查询的输出
  • 的sql_mode = only_full_group_by

另见