Convert SQL Server query to MySQL

Possible Duplicate:
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.

Solution 1

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 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply