gorm
gorm copied to clipboard
How to select all the columns in Join query?
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)
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 ✨
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)
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 ✨
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