Skip to content

Filter query by column in joined table #891

Discussion options

You must be logged in to vote

Using an InnerJoin means we filter out the records that don't match our WHERE criteria.

func queryForDogs() {
	dogs, err := models.Dogs(
		// eager load the dog's hooman
		qm.Load(models.DogRels.Human),
		qm.InnerJoin(
			fmt.Sprintf(
				"%s on %s.%s = %s.%s",
				models.TableNames.Humans,
				models.TableNames.Humans,
				models.HumanColumns.ID,
				models.TableNames.Dogs,
				models.DogColumns.HumanID,
			),
		),
		qm.Where(
			fmt.Sprintf(
				"%s.%s = ?", 
				models.TableNames.Humans,
				models.HumanColumns.Name,
			),
			humanNames[0],
		),
		qm.Limit(5),
	).All(ctx, exec)
}

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@firminochangani
Comment options

Answer selected by kevinmichaelchen
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants