sql - categories for each data in database by a specific id and only allow get 2 for each categories mysql -


possible duplicated mysql query: select top 3 rows table each category. i'm not clear answer why asked again.

this database

+-------+----------+-------------+ |  id   |  app_id  |   content   | +-------+----------+-------------+ |   1   |    19    | hello peer  | +-------+----------+-------------+ |   2   |    20    | hello peer  | +-------+----------+-------------+ |   3   |    19    | hello peer  | +-------+----------+-------------+ |   4   |    19    | hello peer  | +-------+----------+-------------+ |   5   |    21    | hello peer  | +-------+----------+-------------+ |   6   |    20    | hello peer  | +-------+----------+-------------+ |   7   |    19    | hello peer  | +-------+----------+-------------+ |   8   |    20    | hello peer  | +-------+----------+-------------+ |   9   |    21    | hello peer  | +-------+----------+-------------+ 

now how can select record categories app_id , limit 2 each categories , order appid desc

if statement above not clear.here expected output record database above.

+-------+----------+-------------+ |  id   |  app_id  |   content   | +-------+----------+-------------+ |   4   |    19    | hello peer  | +-------+----------+-------------+ |   7   |    19    | hello peer  | +-------+----------+-------------+ |   6   |    20    | hello peer  | +-------+----------+-------------+ |   8   |    20    | hello peer  | +-------+----------+-------------+ |   5   |    21    | hello peer  | +-------+----------+-------------+ |   9   |    21    | hello peer  | +-------+----------+-------------+ 

i want query idea?

you can desired result simpler version as

select c.* category c (   select count(*) category c1   c.app_id = c1.app_id   , c.id < c1.id )<=1 order c.app_id; 

Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -