gorm icon indicating copy to clipboard operation
gorm copied to clipboard

How to select all the columns in Join query?

Open VetchM opened this issue 1 year ago • 1 comments
trafficstars

Let's say I want to join query this two tables:

type A struct{
  Name string
  Age int64
}

type B struct{
  Name string
  Email string
}

How do I write a join query that returns all columns in both table A and table B without manually selecting all columns myself? I tried this but it only return column in table A:

query.A.WithContext(ctx).LeftJoin(query.B, query.A.Name.EqCol(query.B.Name)).Scan(&infos)

VetchM avatar Aug 28 '24 14:08 VetchM

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking

github-actions[bot] avatar Aug 28 '24 14:08 github-actions[bot]

type A struct{
  Name string
  Age int64
}

type B struct{
  Name string
  Email string
}

type C struct{
  Name string
  Age int64
  BName string
  Email string
}


@VetchM you can do it like this:

var c C
query.A.WithContext(ctx).
  LeftJoin(query.B, query.A.Name.EqCol(query.B.Name)).
  Select(query.A.ALL, query.B.ALL).
  Scan(&c)

if you need query two name, you can set alias like this:

var c C
query.A.WithContext(ctx).
  LeftJoin(query.B, query.A.Name.EqCol(query.B.Name)).
  Select(query.A.ALL, query.B.Name.As("BName")).
  Scan(&c)

zcyc avatar Aug 30 '24 11:08 zcyc

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking

github-actions[bot] avatar Aug 30 '24 11:08 github-actions[bot]

type A struct{
  Name string
  Age int64
}

type B struct{
  Name string
  Email string
}

type C struct{
  Name string
  Age int64
  BName string
  Email string
}

@VetchM you can do it like this:

var c C
query.A.WithContext(ctx).
  LeftJoin(query.B, query.A.Name.EqCol(query.B.Name)).
  Select(query.A.ALL, query.B.ALL).
  Scan(&c)

if you need query two name, you can set alias like this:

var c C
query.A.WithContext(ctx).
  LeftJoin(query.B, query.A.Name.EqCol(query.B.Name)).
  Select(query.A.ALL, query.B.Name.As("BName")).
  Scan(&c)

Thanks! Close this issue now

VetchM avatar Sep 02 '24 01:09 VetchM