From 48a20a6e9f3f4d26095df82c3337efec6db0a6fc Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Thu, 22 Feb 2018 12:04:12 +0800 Subject: [PATCH] Add SubQuery method --- main.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/main.go b/main.go index 4bbaadab..c26e05c8 100644 --- a/main.go +++ b/main.go @@ -177,6 +177,15 @@ func (s *DB) QueryExpr() *expr { return Expr(scope.SQL, scope.SQLVars...) } +// SubQuery returns the query as sub query +func (s *DB) SubQuery() *expr { + scope := s.NewScope(s.Value) + scope.InstanceSet("skip_bindvar", true) + scope.prepareQuerySQL() + + return Expr(fmt.Sprintf("(%v)", scope.SQL), scope.SQLVars...) +} + // Where return a new relation, filter records with given conditions, accepts `map`, `struct` or `string` as conditions, refer http://jinzhu.github.io/gorm/crud.html#query func (s *DB) Where(query interface{}, args ...interface{}) *DB { return s.clone().search.Where(query, args...).db