Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ORDER BY case #89

Open
riseoflex88 opened this issue May 8, 2019 · 2 comments
Open

ORDER BY case #89

riseoflex88 opened this issue May 8, 2019 · 2 comments

Comments

@riseoflex88
Copy link

I'm looking to do something similar to this... https://stackoverflow.com/questions/6332043/sql-order-by-multiple-values-in-specific-order

Where I already know the specific order to apply before creating the query.

In raw sql this would be written like so...

WHERE
    id IN ('a','b','c') ...
ORDER BY
   CASE id
      WHEN 'a' THEN 3
      WHEN 'b' THEN 1
      WHEN 'c' THEN 2
   END, id
@shadowhand
Copy link
Owner

Latitude doesn't have any helpers to specifically deal with case ... end, but it is possible to do this with expressions.

@riseoflex88
Copy link
Author

I thought so too, but when I tried using express I get the following error:
Argument 1 passed to Latitude\\QueryBuilder\\order() must be of the type string, object given

I can't find many examples of using express in situ, so I've gone off of the snippet here: fb0d2ff

        $orderby_case = "ORDER BY CASE id \n";
        foreach ($programme_ids as $id => $order)
        {
            $orderby_case .= "WHEN '$id' THEN $order \n";
        }
        $orderby_case .= "END, id";

        $query->orderBy(express($orderby_case), 'DESC');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants