LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   MySql last ten entries percentage (https://www.linuxquestions.org/questions/programming-9/mysql-last-ten-entries-percentage-946219/)

dray235 05-21-2012 07:49 PM

MySql last ten entries percentage
 
Hi,

I have a database that I need to extract the last ten entries from and to get a percentage of the how many times the most popular entry was picked from these ten entries.

This is currently getting me the most popular entry from the last 10 entries:

Code:

$sql =  "SELECT data, count( * ) AS Popularity FROM (SELECT data FROM table ORDER BY id DESC LIMIT 10) table GROUP BY data ORDER BY popularity DESC LIMIT 1";
What can I do to get the most popular as a percentage please?

yancek 05-22-2012 06:04 PM

I have a select statement in a php script which does something similar. It select the number of "wins" from a "matches" table from a database including league tennis results and prints them out in an echo statement as a percentage.

PHP Code:

$query"SELECT (SUM(if(W_L='w', 1,0)) / COUNT(*) * 100) FROM matches"

The above only has two options, w or l so the if probably won't work in your case and you will obviously need to use some trial and error to modify it. I found this online somewhere and modified it until it worked. Not being a programmer, I would not be able to explain why this works, but it does. Unfortunately, I haven't a clue about what specifically you could do to get it working in your case. Good luck!


All times are GMT -5. The time now is 12:09 PM.