Skip to main content

Joins

A join follows a relation or any condition you give it. Conditions and ordering can use any joined model. Select turns each joined row into a value of your own.

Join through a relation​

Join follows a *T relation, and JoinMany follows a []T one, giving one result per related row.

rows, err := db.Posts.
Join(&Posts.User).
Where(ant.Eq(&Users.Name, "Alice")).
OrderBy(&Posts.Title).
Select(func(p *Post, u *User) string { return u.Name + ": " + p.Title }).
All(ctx)
// SELECT ... FROM "posts"
// JOIN "users" ON "posts"."user_id" = "users"."id"
// WHERE "users"."name" = $1
// ORDER BY "posts"."title"
titles, err := db.Users.
JoinMany(&Users.Posts).
Select(func(u *User, p *Post) string { return p.Title }).
All(ctx)
// SELECT ... FROM "users" JOIN "posts" ON "users"."id" = "posts"."user_id"

Keep rows with no match​

LeftJoin and LeftJoinMany keep rows with no match, and the missing side is nil.

rows, err := db.Users.
LeftJoinMany(&Users.Posts).
Select(func(u *User, p *Post) string {
if p == nil {
return u.Name + " has no posts"
}

return u.Name + ": " + p.Title
}).
All(ctx)
// SELECT ... FROM "users" LEFT JOIN "posts" ON "users"."id" = "posts"."user_id"

Join on any condition​

JoinOn needs no relation. The other model is a type argument.

rows, err := db.Users.
JoinOn[Post](ant.EqCol(&Users.ID, &Posts.UserID), ant.Eq(&Posts.Published, true)).
Select(func(u *User, p *Post) string { return p.Title }).
All(ctx)
// SELECT ... FROM "users"
// JOIN "posts" ON "users"."id" = "posts"."user_id" AND "posts"."published" = $1

Conditions passed to the join go in the ON clause. On a left join, a published-only condition in ON keeps users with no published posts. The same condition in Where drops them.

LeftJoinOn, RightJoinOn and FullJoinOn work the same way. CrossJoin[Post]() takes no condition and pairs every row with every row:

pairs, err := db.Users.
CrossJoin[Post]().
Select(func(u *User, p *Post) string { return u.Name + "/" + p.Title }).
All(ctx)
// SELECT ... FROM "users" CROSS JOIN "posts"

JoinUsing[Post](&Users.ID) joins on a column both tables have. Call it on a query: db.Users.Query().JoinUsing[Post](&Users.ID).

FULL JOIN depends on the database: PostgreSQL, SQLite and SQL Server have it, MySQL and MariaDB don't. See databases.

Three and four models​

Chain joins. Each step can follow a relation of any model already in the query.

type Comment struct {
ID int64
PostID int64
Text string
Post *Post
}

var Comments = ant.Fields[Comment]()

rows, err := db.Comments.
Join(&Comments.Post).Join(&Posts.User).
Where(ant.Eq(&Users.Name, "Ann")).
Select(func(c *Comment, p *Post, u *User) string {
return u.Name + "/" + p.Title + "/" + c.Text
}).
All(ctx)
// SELECT ... FROM "comments"
// JOIN "posts" ON "comments"."post_id" = "posts"."id"
// JOIN "users" ON "posts"."user_id" = "users"."id"
// WHERE "users"."name" = $1

Join a table with itself​

Each side of a self-join is its own row. The second copy gets an alias.

pairs, err := db.Folders.
Join(&Folders.Parent).
Select(func(child, parent *Folder) string { return parent.Name + "/" + child.Name }).
All(ctx)
// SELECT ... FROM "folders" JOIN "folders" AS "t1" ON "folders"."parent_id" = "t1"."id"

Join a query​

JoinSub joins the results of a query. Field pointers of the joined model refer to its columns.

published := db.Posts.Where(ant.Eq(&Posts.Published, true))

rows, err := ant.
JoinSub(db.Users.OrderBy(&Users.Name), published, ant.EqCol(&Users.ID, &Posts.UserID)).
Select(func(u *User, p *Post) string { return p.Title }).
All(ctx)
// SELECT ... FROM "users"
// JOIN (SELECT ... FROM "posts" WHERE "posts"."published" = $1) AS "posts"
// ON "users"."id" = "posts"."user_id"
// ORDER BY "users"."name"

LeftJoinSub keeps rows with no match. To join a list held in memory, see ant.Values.

The newest rows per parent​

A lateral join runs once per row, so it can read the top few rows for each parent.

newest := db.Posts.Where(ant.EqCol(&Posts.UserID, &Users.ID)).OrderByDesc(&Posts.ID).Limit(2)

rows, err := ant.
JoinLateral(db.Users.OrderBy(&Users.Name), newest).
Select(func(u *User, p *Post) string { return p.Title }).
All(ctx)
// SELECT ... FROM "users"
// JOIN LATERAL (
// SELECT ... FROM "posts"
// WHERE "posts"."user_id" = "users"."id"
// ORDER BY "posts"."id" DESC LIMIT $1
// ) AS "posts" ON 1 = 1
// ORDER BY "users"."name"

LeftJoinLateral keeps users with no posts. Lateral joins work on PostgreSQL and MySQL only. See databases.