mysql 查询每个分组前N条记录 假设存在表movie, 有字段 id, part(地区), mcount(观看次数) 现查询每个地区观看次数最多的3部movie, 则表 ###id虽未存在group列表 但不报错,原因未知… select a.part,a.id, a.mcount from movie a, movie b #下面的where子句产生迪卡尔积并进行筛选, 最大记录与自己产生一条记录(以自身数据为key), 第二位的 将与最大的数据及自身产生两条数据 ……以次类推where
/*第一种实现方法,效率低并且有错误*/ DECLARE @DD DATETIME SET @DD = GETDATE() SELECT a.GoodsID , a.Account , a.LastUpdate FROM dbo.tb_App_Goods a LEFT JOIN dbo.tb_App_Goods b ON a.Account = b.Account AND a.LastUpdate > b.LastUpdate GROUP BY a.GoodsID , a.Account , a
mysql分组取每组前几条记录(排名) 附group by与order by的研究,需要的朋友可以参考下 --按某一字段分组取最大(小)值所在行的数据 复制代码代码如下: /* 数据如下: name val memo a 2 a2(a的第二个值) a 1 a1--a的第一个值 a 3 a3:a的第三个值 b 1 b1--b的第一个值 b 3 b3:b的第三个值 b 2 b2b2b2b2 b 4 b4b4 b 5 b5b5b5b5b5 */ --创建表并插入数据: 复制代码代码如下: create
按name分组取最大的两个val: [比当前记录val大的条数]小于2条:即当前记录为为分组中的前两条 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name,a.val; val from tb where name=a.name order by val desc) order by a.name,a.val; ) order by a.name;
MySQL 获取所有分类和每个分类的前几条记录 比如有文章表 Article(Id,Category,InsertDate) 现在要用SQL找出每种类型中时间最新的前N个数据组成的集合 SELECT A1.* FROM Article AS A1 INNER JOIN (SELECT A.Category,A.InsertDate FROM Article AS A LEFT JOIN Article AS B ON A.Category = B.Category AND A.InsertDat
CREATE TABLE [dbo].[test1]( [program_id] [int] NULL, [person_id] [int] NULL ) ON [PRIMARY] /*查询每组分组中第一条记录*/ select * from test1 as a where a.person_id in ( person_id from test1 where program_id = a.program_id); select * from ( select ROW_NUMBER() ove
先初始化一些数据,表名为 test ,字段及数据为: SQL执行结果为:每个 uid 都只有 3 条记录. SQL语句为: SELECT * FROM test main WHERE (SELECT COUNT(1) FROM test sub WHERE main.uid = sub.uid AND main.gid > sub.gid ) < 3;
SELECT * from ( select end rownum, else @prov:=t.province end prov, t.* from ( money UNION all money UNION all money UNION all money UNION all money UNION all money UNION all money UNION all money ) t,() rn,(select @prov:='') prov ORDER BY t.province
获取当前文件上一条与下一条记录的原理是上一条的sql语句,从news表里按从大到小的顺序选择一条比当前ID小的新闻,下一条的sql语句,从news表里按从小到大的顺序选择一条比当前ID大的新闻. 如果ID是主键或者有索引,可以直接查找: 方法1: 1.select * from table_a where id = (select id from table_a where id < {$id} order by id desc limit 1); 2.select * from table_a
首先把官网示例拿出来: 连接查询比子查询性能更好 3.6.4 The Rows Holding the Group-wise Maximum of a Certain Column Task: For each article, find the dealer or dealers with the most expensive price. Task: For each article, find the dealer or dealers with the most expensive pr
USE tmpgo CREATE TABLE Employee( ID int identity(1,1), EmpName varchar(20), EmpSalary varchar(10), EmpDepartment varchar(20) ); INSERT INTO EmployeeSELECT '张三','5000','开发部' UNION ALLSELECT '李四','2000','销售部' UNION ALLSELECT '王麻子','2500','销售部' UNION AL
sql在不同数据库查询前几条数据 1. ORACLE SELECT * FROM TABLE_NAME WHERE ROWNUM <= N; HQL: from table_name t order by t.create_time desc where rownum<=n; 2. postgres查询前几条记录SQL SELECT * FROM TABLE_NAME LIMIT N; 3. DB2 SELECT * ROW_NUMBER() OVER(ORDER BY COL1 DESC
mysql分组取每组前几条记录(排名) 附group by与order by的研究 http://www.jb51.net/article/31590.htm --按某一字段分组取最大(小)值所在行的数据 代码如下: /* 数据如下: name val memo a 2 a2(a的第二个值) a 1 a1--a的第一个值 a 3 a3:a的第三个值 b 1 b1--b的第一个值 b 3 b3:b的第三个值 b 2 b2b2b2b2 b 4 b4b4 b 5 b5b5b5b5b5 */ --创建表