0

How to limit by percentage of all rows in MySQL?

I was trying this:

SELECT * FROM table_name
ORDER BY column_name ASC
LIMIT (SELECT ROUND(COUNT(id)*0.25) FROM table_name)

I also tried to save query SELECT ROUND(COUNT(id)*0.25) FROM table_name to variable, but it was the same problem. I know that LIMIT require only number, but I don't know how many rows are in table to give specific number to LIMIT. How to do this without LIMIT if it isn't possible with LIMIT?

I need to SELECT only top 25 % of all rows in table.

  • Have a look at this: [Stack Overflow][1] [1]: http://stackoverflow.com/questions/4741239/select-top-x-or-bottom-percent-for-numeric-values-in-mysql – Matt Feb 03 '14 at 16:29

1 Answers1

-2

you can use this code:

SELECT*
FROM    (
    SELECT list.*, @counter := @counter +1 AS counter
    FROM (select @counter:=0) AS initvar, list
    ORDER BY value DESC   
) AS X
where counter <= (10/100 * @counter);
ORDER BY value DESC

quote from this duplicate question

Community
  • 1
  • 1
Jess Stone
  • 677
  • 8
  • 21