I have a search query that gives a set of results based on input parameters, and the result can be sorted (ASC / DESC) based on different parameters: price, duration, etc. (paginated in place and limited to 10 entries)
Now I have a requirement in which, if I have an id , I would like the corresponding entry to be made for sticky at the top in this result set.
Suppose we have a package table as follows:
Package - id - name - mPrice - vPrice - duration // Searching pkg based on Price (in DESC order) where name = Thailand sqlQuery = "SELECT p.id, p.name, p.mPrice, p.vPrice FROM package p WHERE p.name = LOWER('Thailand') ORDER BY (p.mPrice + p.vPrice) DESC LIMIT 10"
Suppose that the complete result set is 20 records with identifiers from 1 to 20. Now I need to return a record with identifier 14 to always be at the top. I came up with the following query, but this does not work:
sqlQuery = "SELECT p.id, p.name, p.mPrice, p.vPrice FROM package p WHERE p.name = LOWER('Thailand') or p.id = 14 ORDER BY CASE WHEN p.id=14 then 0 else (p.mPrice + p.vPrice) end DESC LIMIT 10"
My guess is why this does not work: after the order by clause, the result set is sorted in descending order, which is then truncated to 10 records. A record with id = 14 cannot be part of this truncated set. Is it correct?
How to get record with id = 14 to insert up?
source share