alexandre Posted November 11, 2022 Author Share Posted November 11, 2022 on phpmyadmin it is saying server type mariadb: Server version: 10.4.24-MariaDB - mariadb.org binary distribution Quote Link to comment https://forums.phpfreaks.com/topic/315517-how-to-count-the-number-of-rows-preceding-a-given-row/page/2/#findComment-1602451 Share on other sites More sharing options...
alexandre Posted November 11, 2022 Author Share Posted November 11, 2022 (edited) but i think finally the order seem fine based on what they received and donated , except for the first place in the rank you can see they donated the same amount and the one who received the most is still in second place .. i think it is missing a tiny bit of accurracy still but is very close. Edited November 11, 2022 by alexandre Quote Link to comment https://forums.phpfreaks.com/topic/315517-how-to-count-the-number-of-rows-preceding-a-given-row/page/2/#findComment-1602452 Share on other sites More sharing options...
Barand Posted November 11, 2022 Share Posted November 11, 2022 In that case you need to uncomment the line in the query where it says "required by mariaDB users" Quote Link to comment https://forums.phpfreaks.com/topic/315517-how-to-count-the-number-of-rows-preceding-a-given-row/page/2/#findComment-1602453 Share on other sites More sharing options...
alexandre Posted November 11, 2022 Author Share Posted November 11, 2022 4 minutes ago, Barand said: In that case you need to uncomment the line in the query where it says "required by mariaDB users" yes this worked for bringing back the order as it was but somehow the order is only based on the total received, with the mariadb line commented i think it was closer to take in count the total_donated. Quote Link to comment https://forums.phpfreaks.com/topic/315517-how-to-count-the-number-of-rows-preceding-a-given-row/page/2/#findComment-1602454 Share on other sites More sharing options...
alexandre Posted November 11, 2022 Author Share Posted November 11, 2022 all in all , now this is working fine, i thank you a lot for your help. Quote Link to comment https://forums.phpfreaks.com/topic/315517-how-to-count-the-number-of-rows-preceding-a-given-row/page/2/#findComment-1602455 Share on other sites More sharing options...
Barand Posted November 11, 2022 Share Posted November 11, 2022 The purpose of the subquery is to sort the data into the required order (ORDER BY total_received DESC, total_donated ASC) before applying the rankings. MariaDB ignores ORDER BY in subqueries. The way round it is the large LIMIT value to force it to write to a temporary table on disc. Quote Link to comment https://forums.phpfreaks.com/topic/315517-how-to-count-the-number-of-rows-preceding-a-given-row/page/2/#findComment-1602456 Share on other sites More sharing options...
alexandre Posted November 11, 2022 Author Share Posted November 11, 2022 that is good to know thank you, those are things that normal people would break their head to normally try to understand what create a certain behavior, after that people still wonder why i prefer asking questions on forums than searching on google, this is a pretty good example why. lets say i would never have guessed this by myself. Quote Link to comment https://forums.phpfreaks.com/topic/315517-how-to-count-the-number-of-rows-preceding-a-given-row/page/2/#findComment-1602459 Share on other sites More sharing options...
alexandre Posted November 11, 2022 Author Share Posted November 11, 2022 35 minutes ago, Barand said: The purpose of the subquery is to sort the data into the required order (ORDER BY total_received DESC, total_donated ASC) before applying the rankings. MariaDB ignores ORDER BY in subqueries. The way round it is the large LIMIT value to force it to write to a temporary table on disc. does this means that for example , i want to make a load more button using thise query if i have to set a limit to a super high number, will this make me unable to make this load more button for which i guess i have to separate the number of rows pulled so with a high limit , will be impossible no ? Quote Link to comment https://forums.phpfreaks.com/topic/315517-how-to-count-the-number-of-rows-preceding-a-given-row/page/2/#findComment-1602463 Share on other sites More sharing options...
Barand Posted November 11, 2022 Share Posted November 11, 2022 No problem. Make that query a subquery of your query doing the pagination so that you paginate the results of the query. For example SELECT rank , name , donated , received FROM ( -- THIS IS THE QUERY TO DO THE RANKING - NOW A SUBQUERY SELECT id , name , @seq := @seq + 1 as seq , @rank := CASE WHEN total_received = @prevr THEN CASE WHEN total_donated = @prevd THEN @rank ELSE @seq END ELSE @seq END as rank , @prevr := total_received as received , @prevd := total_donated as donated FROM ( SELECT id , name , total_donated , total_received FROM donation WHERE total_participant = 1 ORDER BY total_received DESC, total_donated ASC LIMIT 18446744073709551615 ) ordered JOIN ( SELECT @rank := 0, @seq := 0, @prevd := 0, @prevr := 0 ) init ) ranked -- END OF RANKING SUBQUERY ORDER BY rank LIMIT ?, ? "; Running that query with LIMIT 0, 4, then LIMIT 4,4 gave 2 pages... Page 1 +------+---------+---------+----------+ | rank | name | donated | received | +------+---------+---------+----------+ | 1 | Dasher | 250 | 500 | | 2 | Cupid | 370 | 400 | | 2 | Prancer | 370 | 400 | | 4 | Comet | 370 | 380 | +------+---------+---------+----------+ Page 2 +------+---------+---------+----------+ | rank | name | donated | received | +------+---------+---------+----------+ | 5 | Dancer | 510 | 200 | | 6 | Vixen | 200 | 100 | | 7 | Donner | 510 | 100 | +------+---------+---------+----------+ Quote Link to comment https://forums.phpfreaks.com/topic/315517-how-to-count-the-number-of-rows-preceding-a-given-row/page/2/#findComment-1602480 Share on other sites More sharing options...
alexandre Posted November 12, 2022 Author Share Posted November 12, 2022 thats awesome , so just to be sure , this query doesnt create the pagination links right ? because i read about an automatic pagination but i cant remember where .. this is kinda frustrating 😅 Quote Link to comment https://forums.phpfreaks.com/topic/315517-how-to-count-the-number-of-rows-preceding-a-given-row/page/2/#findComment-1602487 Share on other sites More sharing options...
Barand Posted November 12, 2022 Share Posted November 12, 2022 8 hours ago, alexandre said: so just to be sure , this query doesnt create the pagination links right ? Right. The links need to be created on your web page, SQL is confined to the DB server. This query fetches the chosen page of results after a link is clicked.. Quote Link to comment https://forums.phpfreaks.com/topic/315517-how-to-count-the-number-of-rows-preceding-a-given-row/page/2/#findComment-1602492 Share on other sites More sharing options...
alexandre Posted November 12, 2022 Author Share Posted November 12, 2022 alright, thank you very much. Quote Link to comment https://forums.phpfreaks.com/topic/315517-how-to-count-the-number-of-rows-preceding-a-given-row/page/2/#findComment-1602494 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.