mysql - How to get top n records group by wildcards -


here table example:

+--------+-------------+ | id     | city_detail | +--------+-------------+ | 1      | 12_hyd_test | | 2      | 13_blr_test | | 3      | 15_blr_test | | 4      | 18_hyd_test | | 5      | 17_coi_test | | 6      | 22_coi_test | | 7      | 62_hyd_test | | 8      | 72_blr_test | | 9      | 92_blr_test | | 10     | 42_hyd_test | | 11     | 21_coi_test | | 12     | 82_coi_test | +--------+-------+-----+ 

from table, how use condition group select

+--------+-------------+ | id     | city_detail | +--------+-------------+ | 12     | 82_coi_test | | 11     | 21_coi_test | | 10     | 42_hyd_test | | 7      | 62_hyd_test | | 9      | 92_blr_test | | 8      | 72_blr_test | +--------+-------+-----+ 

in each city show 2 result (%coi% or %hyd% or '%blr%') order id desc

probably simplest method use variables:

select e.* (select e.*,              (@rn := if(@c = substr(city_detail, 4), @rn + 1,                         if(@c := substr(city_detail, 4), 1, 1                        )              ) seqnum       example e cross join            (select @c := '', @rn := 0) params       order substr(city_detail, 4)      ) e rn <= 2; 

Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -