Using MySQL/MariaDB, I usually do this kind of query below to get the rank of a specific record so that I can display the proper page in an application:
SET @rownum := 0;
SELECT rank
FROM
( SELECT @rownum := @rownum+1 AS rank, ordid
FROM ord
order by ord_status, ordid
) AS derived_table
WHERE ordid = 1234
limit 1;
I used it for years and it usually works just fine.
However, today, I tried to sort the query according to the description of the order status instead of the order status id (field ord_status). So, I had to sort data using the user funtion named getStatusDescription() that I created in my database. Here is my new query:
SET @rownum := 0;
SELECT rank
FROM
( SELECT @rownum := @rownum+1 AS rank, ordid
FROM ord
order by getStatusDescription(ord_status), ordid
) AS derived_table
WHERE ordid = 1234
limit 1;
For an unknown reason, the rank result is wrong and I do not understand why it is not working. Is it possible that there is a problem or a limitation with MariaDB ?
I'm using MariaDB 10.0.17 on a Centos 7 machine as my development plaftform.
For your information, my function getStatusDescription() just receive a parameter (the order status id) then according to the parameter received select the proper varchar(35) field from a specific table then just return it.
Any help is very welcome.
Guylain Plante