
如何用SQL SERVER取分组数据第一条
2022-12-11 · 百度认证:北京惠企网络技术有限公司官方账号

根据table1_id进行分组所得结果:
select * from (select a.id as a_id,a.name,a.time,a.content,b.id as b_id,b.user from table1 a inner join table2 b on a.id = b.table1_ID) new_tbl where b_id in (select min(id) from table2 group by table1_ID)
扩展资料:
注意事项
在SQL Server数据库中,使用top关键字:SELECT TOP number|percent column_name(s) FROM table_name
在MySQL数据库中,使用LIMIT关键字:SELECT column_name(s) FROM table_name LIMIT number
例子:SELECT * FROM Persons LIMIT 1
select bookName from book where price > 20 limit 1;
limit 1;
or
limit 0,1;
在Oracle数据库中,使用ROWNUM关键字:
SELECT column_name(s) FROM table_name WHERE ROWNUM <= number
例子:SELECT * FROM Persons WHERE ROWNUM <= 1