Trait diesel::prelude::OrderDsl [] [src]

pub trait OrderDsl<Expr: Expression>: AsQuery {
    type Output: AsQuery<SqlType = Self::SqlType>;
    fn order(self, expr: Expr) -> Self::Output;
}

Sets the order clause of a query. If there was already a order clause, it will be overridden. The expression passed to order must actually be valid for the query. See also: .desc() and .asc()

Ordering by multiple columns can be achieved by passing a tuple of those columns.

This is automatically implemented for the various query builder types.

Examples

use self::users::dsl::{users, id, name};

let connection = establish_connection();
connection.execute("INSERT INTO users (name) VALUES ('Saul'), ('Steve'), ('Stan')").unwrap();
// load all users' names, ordered by their name descending
let ordered_names: Vec<String> = users.select(name).order(name.desc()).load(&connection).unwrap();
assert_eq!(vec![String::from("Steve"), String::from("Stan"), String::from("Saul")], ordered_names);

connection.execute("INSERT INTO users (name) VALUES ('Stan')").unwrap();
let ordered_name_id_pairs = users.select((name, id)).order((name.asc(), id.desc())).load(&connection).unwrap();
assert_eq!(vec![(String::from("Saul"), 3), (String::from("Stan"), 6), (String::from("Stan"), 5), (String::from("Steve"), 4)], ordered_name_id_pairs);

Associated Types

Required Methods

Implementors