每个重复行减少到一行相同的表

时间:2016-05-24 03:07:41

标签: php mysql comparison

Table Name - Enrolments

 +-----------+------------------+-------------+-------------+-------------+----------------------+
 +  unit_id  +  student_number  +  unit_code  +  course_id  +  status_id  + last_modified_date  +
 +-----------+------------------+-------------+-------------+-------------+----------------------+
 +   20102   +    SN0001        +  HP-306.12  +   BPL-12    +  WITHDRAWN  +   2/8/2016 15:08     +
 +-----------+------------------+-------------+-------------+-------------+----------------------+
 +   20102   +    SN0001        +  HP-306.12  +   BMin-12   +  COMPLETED  +   2/8/2016 15:08     +
 +-----------+------------------+-------------+-------------+-------------+----------------------+
 +   20110  +     SN0002        +  AS-105.11  +   BBS-12    +  WITHDRAWN  +   2/9/2016 12:01     +
 +-----------+------------------+-------------+-------------+-------------+----------------------+
 +   20110   +    SN0002        +  AS-105.11  +   BMin-13   +  WITHDRAWN  +   2/9/2016 15:13     +
 +-----------+------------------+-------------+-------------+-------------+----------------------+

我在表格注册中显示了这些数据,有匹配的unit_id,student_number

2个条件我想在桌面上运行SQL查询:

一个。我想删除重复的'行并且只保留具有status_id widthdrawn的行。

湾但是,如果2'重复'行都撤回了status_id,我只想保留一个。

最终结果应如下表屏幕截图

Table Name - Newly changed table

 +-----------+------------------+-------------+-------------+-------------+----------------------+
+  unit_id  +  student_number  +  unit_code  +  course_id  +  status_id  +  last_modified_date  +
+-----------+------------------+-------------+-------------+-------------+----------------------+
+   20102   +    SN0001        +  HP-306.12  +   BMin-12   +  COMPLETED  +   2/8/2016 15:08     +
+-----------+------------------+-------------+-------------+-------------+----------------------+
+   20110   +    SN0002        +  AS-105.11  +   BMin-13   +  WITHDRAWN  +   2/9/2016 15:13     +
+-----------+------------------+-------------+-------------+-------------+----------------------+

任何人都可以提供帮助吗?

1 个答案:

答案 0 :(得分:0)

您的解决方案

也许像

SELECT * FROM (SELECT * FROM table ORDER BY `status_id`) t1 GROUP BY `unit_id`, `student_number`;

请注意,我自己没有对此查询进行过测试,因此我无法保证其有效,如果没有则报告,请告诉我错误^ _ ^