distinct 与 group by 去重
例如下表格:表名:fruit
id | Name | Price | Num |
1 | 西瓜 | 10 | 2 |
2 | 西瓜 | 11 | 2 |
3 | 香蕉 | 10 | 3 |
4 | 桃子 | 10 | 2 |
当我想获取Name不重复的数据,结果如下
id | Name | Price | Num |
1 | 西瓜 | 10 | 2 |
3 | 香蕉 | 10 | 3 |
4 | 桃子 | 10 | 2 |
如果查询时用 distinct,则无效果,只能用group by.
select * from fruit where id in (select min(id) from fruit group by Name);
select distinct Name,Price,Num from fruit(无效)
只获取一个字段时有效
select distinct Name from fruit