mysql - How do I employ user variables to select x records for each value of a given column? -


edit: i've discovered problem appears when use phpmyadmin execute query, , not when execute same query using php's mysql_query(). (my webhost not provide command-line access.) specifically, when run on table of 65k+ rows, phpmyadmin appears hang, mysql_query() returns expected result.


i have table: products (product_id, shop_id, date)

for each shop_id in table, following query retrieves 10 rows recent dates

 set @num := 0, @shop_id := null;

select shop_id, date ( select shop_id, date, @num := if(@shop_id = shop_id, @num + 1, 1) row_number, @shop_id := shop_id dummy products order shop_id, date desc ) x x.row_number <= 10;

for example, if there 10 distinct shop_ids, , each of these shop_ids appears in @ least 10 rows, query return 100 rows, 10 per shop_id. query works fine.

in addition date , shop_id, select product_id each row. while following query works fine abut 55k rows, when try 65k rows, mysql appears hang indefinitely (at least phpmyadmin interface fails return page of results):

 set @num := 0, @shop_id := null;

select product_id, shop_id, date ( select product_id, shop_id, date, @num := if(@shop_id = shop_id, @num + 1, 1) row_number, @shop_id := shop_id dummy products order shop_id, date desc ) x row_number <= 10;

i believe tenuous grasp of user variables blame, cannot figure out doing wrong.


Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

android - layout with fragment and framelayout replaced by another fragment and framelayout -