Using a subquery in 'FROM' in gorm

后端 未结 6 1453
梦谈多话
梦谈多话 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:36

    FYI – Jinzhu's method doesn't work

    I have subqueries working using this method...

    var somevalue = 1
    row := db.Select("something").Table("first_table").Where("exists(?)", db.Select("a_relationship_to_something").Model(&SecondTable{}).Where("id = ?", somevalue).QueryExpr()).Row()
    var result string
    row.Scan(&result)
    

    I've tested this with row(), rows(), first(), and find(). You can also use both .Table() and .Model() interchangeably as shown in the example.

提交回复
热议问题