mysql - Conditional ordering with priority to a column -
i have table 4 columns:
- productid
- issponsored
- rank
- popularityscore
i want top 20 products ordered rank
, popularityscore
. rank
considered if issponsored = 1
. have tried using case
in order by
clause. like:
select productid producttable order case when issponsored = 1 rank end asc, case when issponsored = 0 popularityscore end desc limit 20;
the problem i'm facing is, since number of rows issponsored = 0
>> number of rows issponsored = 1
, limit 20, mysql fetches rows issponsored = 0
ordered popularityscore
. want sponsored products ordered ranks , supplemented non sponsored products ordered popularity score.
try conditional logic on three:
order (issponsored = 1) desc, (case when issponsored = 1 rank end) asc, popularityscore desc;
the first key expression on boolean: evaluates true (1) or false (0). removed case
third key because redundant -- can put if like.
Comments
Post a Comment