Skip to content

Commit

Permalink
Add QuerySelect::columns method - select multiple columns (SeaQL/se…
Browse files Browse the repository at this point in the history
  • Loading branch information
billy1624 committed Feb 3, 2023
1 parent 5227b62 commit 528a8cc
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion SeaORM/docs/08-advanced-query/01-custom-select.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ assert_eq!(

## Select Some Attributes Only

Use `select_only` and `column` methods together to select only the attributes you want.
Use `select_only` and `column` methods together to select only the attribute you want.

```rust
// Selecting the name column only
Expand All @@ -32,6 +32,35 @@ assert_eq!(
);
```

If you want to select multiple attributes at once.

```rust
assert_eq!(
cake::Entity::find()
.select_only()
.columns([cake::Column::Id, cake::Column::Name])
.build(DbBackend::Postgres)
.to_string(),
r#"SELECT "cake"."id", "cake"."name" FROM "cake""#
);
```

Conditionally select all columns expect a specific column.

```rust
assert_eq!(
cake::Entity::find()
.select_only()
.columns(cake::Column::iter().filter(|col| match col {
cake::Column::Id => false,
_ => true,
}))
.build(DbBackend::Postgres)
.to_string(),
r#"SELECT "cake"."name" FROM "cake""#
);
```

## Select Custom Expressions

Select any custom expression with `column_as` method, it takes any [`sea_query::SimpleExpr`](https://docs.rs/sea-query/*/sea_query/expr/enum.SimpleExpr.html) and an alias. Use [`sea_query::Expr`](https://docs.rs/sea-query/*/sea_query/expr/struct.Expr.html) helper to build `SimpleExpr`.
Expand Down

0 comments on commit 528a8cc

Please sign in to comment.