SelectBuilder builds SQL SELECT statements. NoContextSupport is returned if a db doesn't support Context. Refer Optimizer Hints/Index/Comment for more details, GORM allows hooks AfterFind for a query, it will be called when querying a record, refer Hooks for details, Query single column from database and scan into a slice, if you want to query multiple columns, use Select with Scan instead, Scopes allows you to specify commonly-used queries which can be referenced as method calls, // Select `id`, `name` automatically when querying, // SELECT `id`, `name` FROM `users` LIMIT 10. Where accepts several types for its pred argument: string - SQL expression. Replace returns a new InsertBuilder with the statement keyword set to Use Git or checkout with SVN using the web URL. Queryer is the interface that wraps the Query method. LeftJoin adds a LEFT JOIN clause to the query. // Requesting more than one column, use `Scan` or `Find` like this: db.Scopes(AmountGreaterThan1000, PaidWithCreditCard).Find(&orders), // Find all credit card orders and amount greater than 1000, db.Scopes(AmountGreaterThan1000, PaidWithCod).Find(&orders), // Find all COD orders and amount greater than 1000, db.Scopes(AmountGreaterThan1000, OrderStatus([], // Find all paid, shipped orders that amount greater than 1000, // SELECT count(1) FROM users WHERE name = 'jinzhu' OR name = 'jinzhu 2', // SELECT count(1) FROM users WHERE name = 'jinzhu'; (count), // SELECT COUNT(DISTINCT(`name`)) FROM `users`, // SELECT count(distinct(name)) FROM deleted_users. OrderByClause adds ORDER BY clause to the query. Learn more, "SELECT * FROM users JOIN emails USING (email_id) WHERE deleted_at IS NULL", "INSERT INTO users (name,age) VALUES (?,?),(?,? GroupBy adds GROUP BY expressions to the query. they're used to log you in. QueryContext executes the given query as implemented by database/sql.QueryContext. RunnerNotSet is returned by methods that need a Runner if it isn't set. Select set Select clause for insert query QueryRowContextWith QueryRowContexts the SQL returned by s with db. Related Posts Accessing the Goodreads API with Go Introducing modoc, a Lightweight Framework for Large … using the provided query as a key, PrepareContext delegates down to the underlying PreparerContext and caches the result "SELECT * FROM elephants WHERE name IN ($1,$2)". RowScanner is the interface that wraps the Scan method. Open a text editor and save a file in your work space as example.go, I use Visual Studio Code and the Go (language support) and TabNine package (for amazing intellisense). placeholder with a (possibly different) SQL placeholder. Expr builds an expression from a SQL fragment and arguments. Where will panic if pred isn't any of the above types. // contains filtered or unexported fields, // ... continue building up your query Table: clause.Table{Name: clause.CurrentTable}, // SELECT * FROM `users` FOR SHARE OF `users`. Row wraps database/sql.Row to let squirrel return new errors on Scan. :1, :2, :3). squirrel by Masterminds - Fluent SQL generation for golang. Where adds WHERE expressions to the query. Additionally, executing the output of this Bug fixes will still be merged (slowly). Pkg.go.dev is a new destination for Go discovery & docs. // You use question marks for placeholders... /// ...squirrel replaces them using PlaceholderFormat. squirrel expects. Expressions are ANDed together in the generated SQL. + 5)", "SELECT * FROM users WHERE username IN (?,?,?,?) Delete returns a DeleteBuilder for this StatementBuilderType. NewStmtCache returns a *StmtCache wrapping a PreparerContext that caches Prepared Stmts. // SELECT * FROM users WHERE name = 'non_existing' ORDER BY id LIMIT 1; // INSERT INTO "users" (name, age) VALUES ("non_existing", 20); // user -> User{ID: 112, Name: "non_existing", Age: 20}. It can also return an error. Package squirrel provides a fluent SQL generator. RightJoin adds a RIGHT JOIN clause to the query. Case returns a new CaseBuilder // sql methods in select columns are ok, pkg.go.dev/github.com/Masterminds/squirrel, func Alias(expr Sqlizer, alias string) aliasExpr, func ConcatExpr(parts ...interface{}) concatExpr, func ExecContextWith(ctx context.Context, db ExecerContext, s Sqlizer) (res sql.Result, err error), func ExecWith(db Execer, s Sqlizer) (res sql.Result, err error), func QueryContextWith(ctx context.Context, db QueryerContext, s Sqlizer) (rows *sql.Rows, err error), func QueryWith(db Queryer, s Sqlizer) (rows *sql.Rows, err error), func (a And) ToSql() (string, []interface{}, error), func Case(what ...interface{}) CaseBuilder, func (b CaseBuilder) Else(expr interface{}) CaseBuilder, func (b CaseBuilder) ToSql() (string, []interface{}, error), func (b CaseBuilder) When(when interface{}, then interface{}) CaseBuilder, func NewStmtCacheProxy(db *sql.DB) DBProxyBeginner, func NewStmtCacher(prep PreparerContext) DBProxyContext, func (b DeleteBuilder) Exec() (sql.Result, error), func (b DeleteBuilder) ExecContext(ctx context.Context) (sql.Result, error), func (b DeleteBuilder) From(from string) DeleteBuilder, func (b DeleteBuilder) Limit(limit uint64) DeleteBuilder, func (b DeleteBuilder) Offset(offset uint64) DeleteBuilder, func (b DeleteBuilder) OrderBy(orderBys ...string) DeleteBuilder, func (b DeleteBuilder) PlaceholderFormat(f PlaceholderFormat) DeleteBuilder, func (b DeleteBuilder) Prefix(sql string, args ...interface{}) DeleteBuilder, func (b DeleteBuilder) PrefixExpr(expr Sqlizer) DeleteBuilder, func (b DeleteBuilder) Query() (*sql.Rows, error), func (b DeleteBuilder) QueryContext(ctx context.Context) (*sql.Rows, error), func (b DeleteBuilder) QueryRowContext(ctx context.Context) RowScanner, func (b DeleteBuilder) RunWith(runner BaseRunner) DeleteBuilder, func (b DeleteBuilder) ScanContext(ctx context.Context, dest ...interface{}) error, func (b DeleteBuilder) Suffix(sql string, args ...interface{}) DeleteBuilder, func (b DeleteBuilder) SuffixExpr(expr Sqlizer) DeleteBuilder, func (b DeleteBuilder) ToSql() (string, []interface{}, error), func (b DeleteBuilder) Where(pred interface{}, args ...interface{}) DeleteBuilder, func (eq Eq) ToSql() (sql string, args []interface{}, err error), func (gt Gt) ToSql() (sql string, args []interface{}, err error), func (gtOrEq GtOrEq) ToSql() (sql string, args []interface{}, err error), func (ilk ILike) ToSql() (sql string, args []interface{}, err error), func (b InsertBuilder) Columns(columns ...string) InsertBuilder, func (b InsertBuilder) Exec() (sql.Result, error), func (b InsertBuilder) ExecContext(ctx context.Context) (sql.Result, error), func (b InsertBuilder) Into(from string) InsertBuilder, func (b InsertBuilder) Options(options ...string) InsertBuilder, func (b InsertBuilder) PlaceholderFormat(f PlaceholderFormat) InsertBuilder, func (b InsertBuilder) Prefix(sql string, args ...interface{}) InsertBuilder, func (b InsertBuilder) PrefixExpr(expr Sqlizer) InsertBuilder, func (b InsertBuilder) Query() (*sql.Rows, error), func (b InsertBuilder) QueryContext(ctx context.Context) (*sql.Rows, error), func (b InsertBuilder) QueryRow() RowScanner, func (b InsertBuilder) QueryRowContext(ctx context.Context) RowScanner, func (b InsertBuilder) RunWith(runner BaseRunner) InsertBuilder, func (b InsertBuilder) Scan(dest ...interface{}) error, func (b InsertBuilder) ScanContext(ctx context.Context, dest ...interface{}) error, func (b InsertBuilder) Select(sb SelectBuilder) InsertBuilder, func (b InsertBuilder) SetMap(clauses map[string]interface{}) InsertBuilder, func (b InsertBuilder) Suffix(sql string, args ...interface{}) InsertBuilder, func (b InsertBuilder) SuffixExpr(expr Sqlizer) InsertBuilder, func (b InsertBuilder) ToSql() (string, []interface{}, error), func (b InsertBuilder) Values(values ...interface{}) InsertBuilder, func (lk Like) ToSql() (sql string, args []interface{}, err error), func (lt Lt) ToSql() (sql string, args []interface{}, err error), func (ltOrEq LtOrEq) ToSql() (sql string, args []interface{}, err error), func (neq NotEq) ToSql() (sql string, args []interface{}, err error), func (nilk NotILike) ToSql() (sql string, args []interface{}, err error), func (nlk NotLike) ToSql() (sql string, args []interface{}, err error), func (o Or) ToSql() (string, []interface{}, error), func (r *Row) Scan(dest ...interface{}) error, func QueryRowContextWith(ctx context.Context, db QueryRowerContext, s Sqlizer) RowScanner, func QueryRowWith(db QueryRower, s Sqlizer) RowScanner, func WrapStdSqlCtx(stdSqlCtx StdSqlCtx) RunnerContext, func Select(columns ...string) SelectBuilder, func (b SelectBuilder) Column(column interface{}, args ...interface{}) SelectBuilder, func (b SelectBuilder) Columns(columns ...string) SelectBuilder, func (b SelectBuilder) CrossJoin(join string, rest ...interface{}) SelectBuilder, func (b SelectBuilder) Distinct() SelectBuilder, func (b SelectBuilder) Exec() (sql.Result, error), func (b SelectBuilder) ExecContext(ctx context.Context) (sql.Result, error), func (b SelectBuilder) From(from string) SelectBuilder, func (b SelectBuilder) FromSelect(from SelectBuilder, alias string) SelectBuilder, func (b SelectBuilder) GroupBy(groupBys ...string) SelectBuilder, func (b SelectBuilder) Having(pred interface{}, rest ...interface{}) SelectBuilder, func (b SelectBuilder) InnerJoin(join string, rest ...interface{}) SelectBuilder, func (b SelectBuilder) Join(join string, rest ...interface{}) SelectBuilder, func (b SelectBuilder) JoinClause(pred interface{}, args ...interface{}) SelectBuilder, func (b SelectBuilder) LeftJoin(join string, rest ...interface{}) SelectBuilder, func (b SelectBuilder) Limit(limit uint64) SelectBuilder, func (b SelectBuilder) MustSql() (string, []interface{}), func (b SelectBuilder) Offset(offset uint64) SelectBuilder, func (b SelectBuilder) Options(options ...string) SelectBuilder, func (b SelectBuilder) OrderBy(orderBys ...string) SelectBuilder, func (b SelectBuilder) OrderByClause(pred interface{}, args ...interface{}) SelectBuilder, func (b SelectBuilder) PlaceholderFormat(f PlaceholderFormat) SelectBuilder, func (b SelectBuilder) Prefix(sql string, args ...interface{}) SelectBuilder, func (b SelectBuilder) PrefixExpr(expr Sqlizer) SelectBuilder, func (b SelectBuilder) Query() (*sql.Rows, error), func (b SelectBuilder) QueryContext(ctx context.Context) (*sql.Rows, error), func (b SelectBuilder) QueryRow() RowScanner, func (b SelectBuilder) QueryRowContext(ctx context.Context) RowScanner, func (b SelectBuilder) RemoveLimit() SelectBuilder, func (b SelectBuilder) RemoveOffset() SelectBuilder, func (b SelectBuilder) RightJoin(join string, rest ...interface{}) SelectBuilder, func (b SelectBuilder) RunWith(runner BaseRunner) SelectBuilder, func (b SelectBuilder) Scan(dest ...interface{}) error, func (b SelectBuilder) ScanContext(ctx context.Context, dest ...interface{}) error, func (b SelectBuilder) Suffix(sql string, args ...interface{}) SelectBuilder, func (b SelectBuilder) SuffixExpr(expr Sqlizer) SelectBuilder, func (b SelectBuilder) ToSql() (string, []interface{}, error), func (b SelectBuilder) Where(pred interface{}, args ...interface{}) SelectBuilder, func Expr(sql string, args ...interface{}) Sqlizer, func (b StatementBuilderType) Delete(from string) DeleteBuilder, func (b StatementBuilderType) Insert(into string) InsertBuilder, func (b StatementBuilderType) PlaceholderFormat(f PlaceholderFormat) StatementBuilderType, func (b StatementBuilderType) Replace(into string) InsertBuilder, func (b StatementBuilderType) RunWith(runner BaseRunner) StatementBuilderType, func (b StatementBuilderType) Select(columns ...string) SelectBuilder, func (b StatementBuilderType) Update(table string) UpdateBuilder, func (b StatementBuilderType) Where(pred interface{}, args ...interface{}) StatementBuilderType, func NewStmtCache(prep PreparerContext) *StmtCache, func (sc *StmtCache) Exec(query string, args ...interface{}) (res sql.Result, err error), func (sc *StmtCache) ExecContext(ctx context.Context, query string, args ...interface{}) (res sql.Result, err error), func (sc *StmtCache) Prepare(query string) (*sql.Stmt, error), func (sc *StmtCache) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error), func (sc *StmtCache) Query(query string, args ...interface{}) (rows *sql.Rows, err error), func (sc *StmtCache) QueryContext(ctx context.Context, query string, args ...interface{}) (rows *sql.Rows, err error), func (sc *StmtCache) QueryRow(query string, args ...interface{}) RowScanner, func (sc *StmtCache) QueryRowContext(ctx context.Context, query string, args ...interface{}) RowScanner, func (b UpdateBuilder) Exec() (sql.Result, error), func (b UpdateBuilder) ExecContext(ctx context.Context) (sql.Result, error), func (b UpdateBuilder) Limit(limit uint64) UpdateBuilder, func (b UpdateBuilder) Offset(offset uint64) UpdateBuilder, func (b UpdateBuilder) OrderBy(orderBys ...string) UpdateBuilder, func (b UpdateBuilder) PlaceholderFormat(f PlaceholderFormat) UpdateBuilder, func (b UpdateBuilder) Prefix(sql string, args ...interface{}) UpdateBuilder, func (b UpdateBuilder) PrefixExpr(expr Sqlizer) UpdateBuilder, func (b UpdateBuilder) Query() (*sql.Rows, error), func (b UpdateBuilder) QueryContext(ctx context.Context) (*sql.Rows, error), func (b UpdateBuilder) QueryRow() RowScanner, func (b UpdateBuilder) QueryRowContext(ctx context.Context) RowScanner, func (b UpdateBuilder) RunWith(runner BaseRunner) UpdateBuilder, func (b UpdateBuilder) Scan(dest ...interface{}) error, func (b UpdateBuilder) ScanContext(ctx context.Context, dest ...interface{}) error, func (b UpdateBuilder) Set(column string, value interface{}) UpdateBuilder, func (b UpdateBuilder) SetMap(clauses map[string]interface{}) UpdateBuilder, func (b UpdateBuilder) Suffix(sql string, args ...interface{}) UpdateBuilder, func (b UpdateBuilder) SuffixExpr(expr Sqlizer) UpdateBuilder, func (b UpdateBuilder) Table(table string) UpdateBuilder, func (b UpdateBuilder) ToSql() (string, []interface{}, error), func (b UpdateBuilder) Where(pred interface{}, args ...interface{}) UpdateBuilder.