如何将子查询转换为 GORM 中的连接查询?

huangapple go评论86阅读模式
英文:

How can I transform a subquery to join in gorm?

问题

我正在使用GORM,并且有以下这些模型:

type User struct {
  ID    uint
  UUID  uuid.UUID
  Email string
}

type Profile struct {
  ID     uint
  UUID   uuid.UUID
  Domain string
  UserID uuid.UUID
  User   User `gorm:"references:UUID"`
}

现在我想找到所有具有域名为example.com的配置文件的用户。

我已经尝试了一些"Join"查询,但是没有成功。不过,我通过使用子查询成功实现了它:

var users []users

DB.Where(
  "uuid IN (?)",
  DB.Select("user_id").Where("domain = ?", "example.com").Table("profiles")
).Find(&users)

但我觉得这不是一种很优雅的方式。我认为使用"Join"查询会更直观。如何将这个子查询转换为"Join"查询?

谢谢!

英文:

I am using GORM and I have these models:

type User struct {
  ID    uint
  UUID  uuid.UUID
  Email string
}

type Profile struct {
  ID     uint
  UUID   uuid.UUID
  Domain string
  UserID uuid.UUID
  User   User `gorm:"references:UUID"`
}

Now I want to find all users that have a profile with domain e.g. example.com.

I already tried some "Join" queries but I did not get it to work. However I managed to get it working by using a subquery:

var users []users

DB.Where(
  "uuid IN (?)",
  DB.Select("user_id").Where("domain = ?", "example.com").Table("profiles")
).Find(&users)

But I don't think this is a pretty elegant way. I think a join would be more straight forward. How do I convert this subquery to a join query?

Thanks!

答案1

得分: 2

尝试这个:

DB.Select("u.*").Table("users u").Joins("INNER JOIN profiles p on p.user_id = u.uuid").Where("p.domain = ?", "example.com").Find(&users)

这将得到以下结果:

SELECT u.* FROM users u INNER JOIN profiles p on p.user_id = u.uuid WHERE p.domain = "example.com"
英文:

Try this

DB.Select("u.*").Table("users u").Joins("INNER JOIN profiles p on p.user_id = u.uuid").Where("p.domain = ?", "example.com").Find(&users)

this will result:

SELECT u.* FROM users u INNER JOIN profiles p on p.user_id = u.uuid WHERE p.domain = "example.com"

答案2

得分: 2

如果你更喜欢使用gorm内置的功能而不是原始查询连接,你可以尝试这样做:

profile := &Profile{Domain: "example.com"}
user := &User{}

db.Select("User.*").Joins("User").Model(&Profile{}).Where(profile).Find(&user)

如果我们像这样使用gorm的调试模式:

db.Debug().Select("User.*").Joins("User").Model(&Profile{}).Where(profile).Find(&user)

SQL查询日志将会是这样的:

SELECT User.*,`User`.`id` AS `User__id`,`User`.`uuid` AS `User__uuid`,`User`.`email` AS `User__email` FROM `profiles` LEFT JOIN `users` `User` ON `profiles`.`user_id` = `User`.`uuid` WHERE `profiles`.`domain` = 'example.com'
英文:

If you prefer using gorm bulit-in feature instead of raw query join, you can try this:

profile := &Profile{Domain: "example.com"}
user := &User{}

db.Select("User.*").Joins("User").Model(&Profile{}).Where(profile).Find(&user)

If we use gorm Debuging mode like this:

db.Debug().Select("User.*").Joins("User").Model(&Profile{}).Where(profile).Find(&user)

The SQL query log will be like this:

SELECT User.*,`User`.`id` AS `User__id`,`User`.`uuid` AS `User__uuid`,`User`.`email` AS `User__email` FROM `profiles` LEFT JOIN `users` `User` ON `profiles`.`user_id` = `User`.`uuid` WHERE `profiles`.`domain` = 'example.com'

huangapple
  • 本文由 发表于 2022年1月9日 20:11:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/70641120.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定