ordering using SQL's CASE WHEN/THEN syntax django

后端 未结 1 765
春和景丽
春和景丽 2021-01-17 05:19

I\'m trying to order contacts in table and I\'m facing some problems, I found a nice solution in this SO question, maybe it\'s an over kill for this, but I\'m trying to do t

1条回答
  •  醉梦人生
    2021-01-17 05:56

    That question you have linked to is from 2012. A lot has changed since then, such as the inclusion of ..... CASE/WHEN in django.

    A Case() expression is like the if ... elif ... else statement in Python. Each condition in the provided When() objects is evaluated in order, until one evaluates to a truthful value. The result expression from the matching When() object is returned.

    The whole idea being that you don't need to write complicated queries as people sometimes had to in the past.

    The standard practice is to create an annotation with CASE/WHEN and then use that in annotation in the order by

    0 讨论(0)
提交回复
热议问题