分析一条sql语句的意思----------------
是mysql的
demo 表name val两列 按name分组 取分组后每组的前两个值
下面的sql语句是正确的,我有试过,就是不懂它的意思
求解。。。。。。
select a.* from demo a where 2 > ( select count(*) from demo where name = a.name and val > a.val )
--1select a.*,(select count(*) from demo where name = a.name and val > a.val )from demo a where 2 > ( select count(*) from demo where name = a.name and val > a.val ) --2select * from (select a.*,(select count(*) from demo where name = a.name and val > a.val ) as cnt) t where cnt >2
[解决办法]
就是说 a 表中的每一行 与 demo 表比较 :
name = a.name and val > a.val 这个是关联条件
这个
select count(*) from demo where name = a.name and val > a.val
在name相同的情况下,返回当整张表VAL比当前行VAL大的个数