SQL Query通过使两列数据唯一来获取不同的行

时间:2014-07-23 10:27:07

标签: mysql sql mysql-workbench

Id  uid pid   weekNo  year
1    1  1      28    2014 
2    1  2      28    2014
3    1  3      29    2014
4    1  4      28    2015
5    1  5      28    2015 

//输出应该是这样,即将weekno和year视为不同

Id  uid pid   weekNo  year
1    1  1      28    2014 
3    1  3      29    2014
4    1  4      28    2015

3 个答案:

答案 0 :(得分:1)

如果你不关心id的值,uid,pid应该来自同一行而不是使用下面的查询来返回不同的年份和周年组

SELECT MIN(Id),MIN(uid),MIN(pid),weekno,year
FROM tableName
GROUP BY weekno,year

答案 1 :(得分:1)

如果您想保证所有值都来自相同的行,那么您需要一个查询,例如:

select t.*
from table t
where not exists (select 1
                  from table t2
                  where t2.weekno = t.weekno and t2.year = t.year and t2.pid > t.pid
                 );

答案 2 :(得分:-1)

从tableName GROUP BY weekno,year;

中选择*