()
| 381 | } |
| 382 | |
| 383 | func ExampleDB_Model_selectGroupBy() { |
| 384 | db := modelDB() |
| 385 | |
| 386 | var res []struct { |
| 387 | AuthorId int |
| 388 | BookCount int |
| 389 | } |
| 390 | |
| 391 | err := db.Model(&Book{}). |
| 392 | Column("author_id"). |
| 393 | ColumnExpr("count(*) AS book_count"). |
| 394 | Group("author_id"). |
| 395 | OrderExpr("book_count DESC"). |
| 396 | Select(&res) |
| 397 | if err != nil { |
| 398 | panic(err) |
| 399 | } |
| 400 | fmt.Println("len", len(res)) |
| 401 | fmt.Printf("author %d has %d books\n", res[0].AuthorId, res[0].BookCount) |
| 402 | fmt.Printf("author %d has %d books\n", res[1].AuthorId, res[1].BookCount) |
| 403 | // Output: len 2 |
| 404 | // author 1 has 2 books |
| 405 | // author 11 has 1 books |
| 406 | } |
| 407 | |
| 408 | func ExampleDB_Model_selectWith() { |
| 409 | authorBooks := pgdb.Model(&Book{}).Where("author_id = ?", 1) |
nothing calls this directly
no test coverage detected
searching dependent graphs…