0

I have find an answer to my query problem from Solution However, I am problem in understanding the logic of the query. Can anyone help me understand this query?

Query:

select a.*
from Tablename a
where 
(
   select count(*) 
   from Tablename as b
   where a.group = b.group and a.id >= b.id
) <= 2
Community
  • 1
  • 1

1 Answers1

0

Mysql will first form the query op of

select count(*) 
   from Tablename as b
   where a.group = b.group and a.id >= b.id

this will count the record of table name Tablename on condition that a.group column value is equal to b.group column value and a.id's value greater than b.id's value, it will count the record which satisfy this condition.

then it passes it to main / parent query it's op. now parent query will count the record from Tablename on condition that subquery's op less than or equal to 2.

And why are you querying on same table (in main + sub query) ? and where's the b.group came from ?

user3145373 ツ
  • 7,858
  • 6
  • 43
  • 62
  • Basically, I want to get the last two entries of each of the group. I am trying to understand this concept but "a.id >= b.id" is confusing me a little. How this condition is impacting on the results? – Novice_Programmer Jun 14 '14 at 08:30