Using a subquery in 'FROM' in gorm

后端 未结 6 1450
梦谈多话
梦谈多话 2021-02-10 03:03

I would like to know how I can use a subquery in FROM clause using gorm. It would look like the following:

SELECT * FROM 
(
  SELECT foo.*
  FROM foo
  WHERE bar         


        
6条回答
  •  青春惊慌失措
    2021-02-10 03:24

    You could use QueryExpr, refer

    http://jinzhu.me/gorm/crud.html#subquery

    db.Where("amount > ?", DB.Table("orders").Select("AVG(amount)").Where("state = ?", "paid").QueryExpr()).Find(&orders)

    Which generate SQL

    SELECT * FROM "orders" WHERE "orders"."deleted_at" IS NULL AND (amount > (SELECT AVG(amount) FROM "orders" WHERE (state = 'paid')));

提交回复
热议问题