sql过滤重复数据
昨天碰到一个Case: 数据库表tbProduct里有如下数据:
| id | company | product |
|---|---|---|
| 1 | chenreal | ibm pc |
| 2 | realme | ms mouse |
| 3 | real | 11111 |
| 4 | chenreal | ddddd |
| 5 | chenreal | cccc |
| 6 | realme | qqqqq |
| 7 | realme | cccc |
| 8 | real | 1112222 |
| 9 | chenreal | 88888 |
要求列出最新的数据,但如果company重复的需要过滤掉 简单的DISTINCT无法实现的,这里需要用到 GROUP BY
我的解决方法:
select * from tbProduct where [id] in (select max([id]) from tbProduct group by company) order by [id] desc