Select [PostgreSQL MySQL]

API

For the full list of supported methods, see SelectQuerySELECT - 图1open in new window.

  1. db.NewSelect().
  2. With("cte_name", subquery).
  3. Model(&strct).
  4. Model(&slice).
  5. Column("col1", "col2"). // quotes column names
  6. ColumnExpr("col1, col2"). // arbitrary unsafe expression
  7. ColumnExpr("count(*)").
  8. ColumnExpr("count(?)", bun.Ident("id")).
  9. ColumnExpr("(?) AS alias", subquery).
  10. ExcludeColumn("col1"). // all columns except col1
  11. ExcludeColumn("*"). // exclude all columns
  12. Table("table1", "table2"). // quotes table names
  13. TableExpr("table1 AS t1"). // arbitrary unsafe expression
  14. TableExpr("(?) AS alias", subquery).
  15. ModelTableExpr("table1 AS t1"). // overrides model table name
  16. Join("JOIN table2 AS t2 ON t2.id = t1.id").
  17. Join("LEFT JOIN table2 AS t2").JoinOn("t2.id = t1.id").
  18. WherePK(). // where using primary keys
  19. Where("id = ?", 123).
  20. Where("name LIKE ?", "my%").
  21. Where("? = 123", bun.Ident("id")).
  22. Where("id IN (?)", bun.In([]int64{1, 2, 3})).
  23. Where("id IN (?)", subquery).
  24. Where("FALSE").WhereOr("TRUE").
  25. WhereGroup(" AND ", func(q *bun.SelectQuery) *bun.SelectQuery {
  26. return q.WhereOr("id = 1").
  27. WhereOr("id = 2")
  28. }).
  29. Group("col1", "col2"). // quotes column names
  30. GroupExpr("lower(col1)"). // arbitrary unsafe expression
  31. Order("col1 ASC", "col2 DESC"). // quotes column names
  32. OrderExpr("col1 ASC NULLS FIRST"). // arbitrary unsafe expression
  33. Having("column_name > ?", 123).
  34. Limit(100).
  35. Offset(100).
  36. For("UPDATE").
  37. For("SHARE").
  38. Scan(ctx)

Example

To select into a struct, define a model and use SelectQuerySELECT - 图2open in new window:

  1. book := new(Book)
  2. err := db.NewSelect().Model(book).Where("id = ?", 123).Scan(ctx)

Count rows

Bun provides CountSELECT - 图3open in new window helper to generate count(*) queries:

  1. count, err := db.NewSelect().Model((*User)(nil)).Count(ctx)

Because selecting and counting rows is a common operation, Bun also provides ScanAndCountSELECT - 图4open in new window:

  1. var users []User
  2. count, err := db.NewSelect().Model(&users).Limit(20).ScanAndCount(ctx)
  3. if err != nil {
  4. panic(err)
  5. }
  6. fmt.Println(users, count)

EXISTS

You can also use ExistsSELECT - 图5open in new window helper to use the corresponding EXISTS SQL operator:

  1. exists, err := db.NewSelect().Model((*User)(nil)).Where("name LIKE '%foo%'").Exists(ctx)
  2. if err != nil {
  3. panic(err)
  4. }
  5. if !exists {
  6. fmt.Println("such user does not exist")
  7. }
  1. SELECT EXISTS (SELECT * FROM users WHERE name LIKE '%foo%')

Joins

To select a book and manually join the book author:

  1. book := new(Book)
  2. err := db.NewSelect().
  3. Model(book).
  4. ColumnExpr("book.*").
  5. ColumnExpr("a.id AS author__id, a.name AS author__name").
  6. Join("JOIN authors AS a ON a.id = book.author_id").
  7. OrderExpr("book.id ASC").
  8. Limit(1).
  9. Scan(ctx)
  1. SELECT book.*, a.id AS author__id, a.name AS author__name
  2. FROM books
  3. JOIN authors AS a ON a.id = book.author_id
  4. ORDER BY book.id ASC
  5. LIMIT 1

To generate complex joins, use JoinOn:

  1. q = q.
  2. Join("JOIN authors AS a").
  3. JoinOn("a.id = book.author_id").
  4. JoinOn("a.deleted_at IS NULL")
  1. JOIN authors AS a ON a.id = book.author_id AND a.deleted_at IS NULL

Subqueries

You can use Bun queries (including INSERT, UPDATE, and DELETE queries) as a subquery:

  1. subq := db.NewSelect().Model((*Book)(nil)).Where("author_id = ?", 1)
  2. err := db.NewSelect().Model().TableExpr("(?) AS book", subq).Scan(ctx, &books)
  1. SELECT * FROM (
  2. SELECT "book"."id", "book"."title", "book"."text"
  3. FROM "books" AS "book" WHERE (author_id = 1)
  4. ) AS book