Mysql – How to force feed offset keyword a number calculated from table

MySQL

I am trying something as shown here.

As in this sql fiddle http://sqlfiddle.com/#!9/8d217/4 I created table

CREATE TABLE something (`some_column` int);
INSERT INTO something (`some_column`) VALUES (10),(20),(30),(40),(50);

I am trying to find median value so following is the query

select some_column from something order by some_column limit 1 offset 
(select floor(count(some_column) / 2) from something);

MySQL complains about the query part after offset; and it does accept hard coded values like any integer instead of sub-query like (select floor(count(some_column) / 2) from something).

I am guessing the offset expects to get hard coded value and not a column with a value. I did try other options like using variables @ but cannot make it work.

My question is; if I want to use a value from query operation in offset or limit and not a hard coded value, how should I go about it?

Best Answer

Plan A: Write a stored procedure to construct (via CONCAT()) the query you need, then use prepare and executed.

Plan B: Create a temp table with an AUTO_INCREMENT column in order to number the rows. Then look in that table for the answer.

Plan C: Use @variables to count through the table.

Plan D: http://www.artfulsoftware.com/infotree/queries.php#444