Select TOP X (or bottom) percent for numeric values in MySQL
How would I write this query in MySQL??
SELECT TOP 50 PERCENT * FROM Persons
The number of entries in the table will be constantly changing so I need to select by percentage rather than a fixed number.
Here is Solutions:
We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.
This is actually a duplicate question, but I now have a better, single-pass, fast, answer:
SELECT * FROM ( SELECT tbl.*, @counter := @counter +1 counter FROM (select @counter:=0) initvar, tbl ORDER BY ordcolumn ) X WHERE counter <= (50/100 * @counter) ORDER BY ordcolumn;
Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂