gocraft/dbr:如何使用多个条件进行JOIN操作?

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

gocraft/dbr: How to JOIN with multiple conditions?

问题

我使用golang开发Web应用程序。
我使用gocraft/dbr库作为O/R映射器。

我有两个表:image和entry。
我将它们的表连接起来,我想获取image_url。

type Image struct {
    ImageUrl   dbr.NullString `db:"image_url"`
}

type Entry struct {
    CompanyImageID   dbr.NullInt64  `db:"company_image_id"`
    CompanyImage Image
    EyecatchIamgeID dbr.NullInt64  `db:"eyecatch_image_id"`
    EyecatchImage Image
}

然后我尝试了以下代码:

var entry Entry
sess.Select("*").From("entry").
    LeftJoin(dbr.I("image").As("eyecatch_image"), "entry.eyecatch_image_id = eyecatch_image.id").
    LeftJoin(dbr.I("image").As("company_image"), "entry.company_image_id = company_image.id").
    Load(&entry)
log.Println("company:", entry.CompanyImage)
log.Println("eyecatch:", entry.EyecatchImage)  

结果为:

company: {{{https://company_image_url.png true}}}
eyecatch: {{{ false}}}

我期望的结果是:

company: {{{https://company_image_url.png true}}}
eyecatch: {{{{http://eyecatch_image_url.png true}}}

当我尝试更改连接条件如下所示时:

sess.Select("*").From("entry").
    LeftJoin(dbr.I("image").As("eyecatch_image"), "entry.eyecatch_image_id = eyecatch_image.id").
    Load(&entry)

结果为:

company: {{{http://eyecatch_image_url.png true}}}
eyecatch: {{{ false}} {{ false}}}}

我不知道如何在多个条件下使用连接。

谢谢。

英文:

I develop web application with golang.
I use the library gocraft/dbr as O/R Mapper.

I have two tables: image and entry.
I join their tables and I want to get image_url.

type Image struct {
    ImageUrl   dbr.NullString `db:"image_url"`
}

type Entry struct {
    CompanyImageID   dbr.NullInt64  `db:"company_image_id"`
	CompanyImage Image
	EyecatchIamgeID dbr.NullInt64  `db:"eyecatch_image_id"`
	EyecatchImage Image
}

Then I tried below:

var entry Entry
sess.Select("*").From("entry").
    LeftJoin(dbr.I("image").As("eyecatch_image"), "entry.eyecatch_image_id = eyecatch_image.id").
	LeftJoin(dbr.I("image").As("company_image"), "entry.company_image_id = company_image.id").
    Load(&entry)
log.Println("company:", entry.CompanyImage)
log.Println("eyecatch:", entry.EyecatchImage)	

result:

company: {{{https://company_image_url.png true}}}
eyecatch: {{{ false}}}

I expect below, but it did not become as expected.

company: {{{https://company_image_url.png true}}}
eyecatch: {{{{http://eyecatch_image_url.png true}}}

When I tried to change join condition like below:

sess.Select("*").From("entry").
    LeftJoin(dbr.I("image").As("eyecatch_image"), "entry.eyecatch_image_id = eyecatch_image.id")
	Load(&entry)

result:

company: {{{http://eyecatch_image_url.png true}}}
eyecatch: {{{ false}} {{ false}}}}

I don't know how to use join with multiple conditions.

Thank you.

答案1

得分: 1

文档真的很差劲 - 似乎他们放弃了发布这个库的想法。有一个开放的拉取请求提供了稍微好一点的文档。 在这里,作者描述了如何创建一个多条件的示例:

cond:= dbr.And(
  dbr.Or(
    dbr.Gt("created_at", "2015-09-10"),
    dbr.Lte("created_at", "2015-09-11"),
  ),
  dbr.Eq("title", "hello world"),
)

然后在任何语句中使用这个条件:

sess.Select("*").From("entry").
    LeftJoin(dbr.I("image").As("eyecatch_image"), cond)
    Load(&entry)
英文:

The documentation is really poor - it seems they gave up on the idea of publishing the library. There is an open pull request that provides a little better documentation.. Here the author describes that you can create a multiple condition like this:

cond:= dbr.And(
  dbr.Or(
    dbr.Gt("created_at", "2015-09-10"),
    dbr.Lte("created_at", "2015-09-11"),
  ),
  dbr.Eq("title", "hello world"),
)

Then use the condition in any statement:

sess.Select("*").From("entry").
    LeftJoin(dbr.I("image").As("eyecatch_image"), cond)
    Load(&entry)

huangapple
  • 本文由 发表于 2016年2月19日 10:53:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/35496485.html
匿名

发表评论

匿名网友

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

确定