S
S
Salavat Sitdikov2012-04-09 09:30:22
MySQL
Salavat Sitdikov, 2012-04-09 09:30:22

Is it possible to do this with one sql query?

There are authors of materials.
Each author has several entries.
It is necessary to make a query so that n records are selected for each author (if there are fewer, then how many there are).
I know how to do this in several requests, but in one it does not work.
I thought in the direction of GROUP_CONCAT, but you cannot impose a condition on it to limit the number of selected elements. Is it even possible?
Thank you all for your help!

Answer the question

In order to leave comments, you need to log in

4 answer(s)
T
tnz, 2012-04-09
@zona7o

Selecting the last few entries in some groups

S
ssbxlan, 2012-04-09
@ssbxlan

here is a similar question

R
rPman, 2012-04-09
@rPman

Sometimes it is more efficient to pre-number user records in a separate field (once the old data and then renumber them again when adding and deleting records), then the query becomes very simple:

select * from статьи t where t.номер<=:limit

Z
ztxn, 2012-04-09
@ztxn

It is possible But the decision with several requests from the client will be much more productive. Perhaps there is another way to apply the trick with variables ...
select * from статьи t
where :n > (select count(*) from статьи s where s.автор = t.автор and s.id<t.id)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question