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
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.