sql语句查询分类前几条信息
1.SQL2005,情况下使用 行号 Row_Number()
SELECT *
FROM
(
SELECT ROW_NUMBER() OVER(PARTITION BY ct ORDER BY name) AS rnk,*
FROM tb_name
) AS t
WHERE rnk<=3
讲解:用ROW_NUMBER() 以ct字段划分表数据,为每行数据添加行号。然后从这个结果集里查询出行号小于等于3的记录,就是每类的前3条记录。
sql语句查询分类前几条信息
1.SQL2005,情况下使用 行号 Row_Number()
SELECT *
FROM
(
SELECT ROW_NUMBER() OVER(PARTITION BY ct ORDER BY name) AS rnk,*
FROM tb_name
) AS t
WHERE rnk<=3
讲解:用ROW_NUMBER() 以ct字段划分表数据,为每行数据添加行号。然后从这个结果集里查询出行号小于等于3的记录,就是每类的前3条记录。