MCPcopy Create free account
hub / github.com/go-pg/pg / ExampleDB_Model_selectGroupBy

Function ExampleDB_Model_selectGroupBy

example_model_test.go:383–406  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

381}
382
383func 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
408func ExampleDB_Model_selectWith() {
409 authorBooks := pgdb.Model(&Book{}).Where("author_id = ?", 1)

Callers

nothing calls this directly

Calls 8

modelDBFunction · 0.85
OrderExprMethod · 0.80
GroupMethod · 0.80
ColumnExprMethod · 0.80
ColumnMethod · 0.80
ModelMethod · 0.65
PrintfMethod · 0.65
SelectMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…