比如现有一张表user:
id name age
1 a 10
2 b 15
3 c 20
4 a 10
5 b 15
6 a 15
7 b 10
要求按name和age都相同的去重后统计条目,如上表去重后总行数应该为5。
最初设计的sql语句为select count(name) from (select name,age,count(name) from user group by name,age);
结果运行时报错:every derived table must have its own alias。意思是每一个派生出的表格都要有一个别名,于是我们给去重后的表格起个别名subtable,修改后的sql语句如下:
select count(name) from (select name,age,count(name) from user group by name,age)as subtable;