英文:
Golang: Query with JOIN using GO-PG package
问题
我需要帮助你处理go-pg包和重构你的查询。你有两个表格:
学生表的定义:
type Student struct {
ID int `json:"id"`
UserID int `json:"user_id"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
}
团队表的定义:
type Team struct {
ID int `json:"id"`
StudentID int `json:"student_id"`
TeamName string `json:"team_name"`
CreateDate time.Time `json:"create_date"`
}
我有以下方法,我相信我可以改进它。我不知道如何改进,但是每次使用JOIN查询调用基础方法都不是最好的解决方案,我完全确定。所以这是我的方法:
type Student struct {
ID int `json:"id"`
UserID int `json:"user_id"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
}
type Team struct {
ID int `json:"id"`
StudentID int `json:"student_id"`
TeamName string `json:"team_name"`
CreateDate time.Time `json:"create_date"`
}
type StudentInTeam struct {
UserID int `json:"id"`
InTeam bool `json:"in_team"`
}
func (p *storage) FindStudentsInTeam(ids []int, teamID int) (map[int]bool, error) {
studentsInTeam := make(map[int]bool, len(ids))
for _, id := range ids {
res, err := p.Model((*Team)(nil)).
Where("create_date <= ?", time.Now().UTC()).
Where("team_id = ?", teamID).
Join("JOIN students ON students.id = student_id").
Where("students.user_id = ?", id).
Exists()
if err != nil {
return nil, err
}
studentsInTeam[id] = res
}
return studentsInTeam, nil
}
非常感谢任何帮助或建议。祝你有愉快的一天!
英文:
I need help with go-pg package and refactoring my query.
I have 2 tables
Table definition for Student :
type Student {
ID int `json:"id"`
UserID int `json:"user_id"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
}
Table definition for Teams :
type Teams {
ID int `json:"id"`
StudentID int `json:"strudent_id"`
TeamName string `json:"team_name"`
CreateDate time.Time `json:"create_date"`
}
I have the following method which i am sure i can improve. I don't know how, but every time make a call to the base with JOIN query is not the best solution I'm totally sure
So here is my method
type Student struct{
ID int `json:"id"`
UserID int `json:"user_id"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
}
type Team struct {
ID int `json:"id"`
StudentID int `json:"strudent_id"`
TeamName string `json:"team_name"`
CreateDate time.Time `json:"create_date"`
}
type StudentInTeam struct {
UserID int `json:"id"`
InTeam bool `json:"in_team"`
}
func (p *storage) FindStudentsInTeam (ids []int, teamID int)(map[int]bool, error){
studentsInTeam := make(map[int]bool, len(ids))
for _, id := range ids {
res, err := p.Model((*Team)(nil)).
Where("create_date <= ?", time.Now().UTC()).
Where("team_id = teamID").
Join("JOIN students ON students.id = student_id").
Where("students.user_id = ?", id).
Exists()
if err != nil {
return nil, err
}
studentsInTeam[id] = res
}
return studentsInTeam, nil
}
Will be appreciate for any help or advise.
Have a nice day!
答案1
得分: 1
在循环内部进行查询并不总是一个好主意。你可以使用IN
子句来检查表中是否存在ids
。
你可以使用Column
方法选择特定的列:
func (q *Query) Column(columns ...string) *Query {
.....
}
你可以按照以下方式重写你的方法:
func (p *storage) FindStudentsInTeam(ids []int, teamID int) (map[int]bool, error) {
// 你的其他代码
var existedStudentTeams []Team
err := p.Model(&existedStudentTeams).
Column("team_id").
Where("create_date <= ?", time.Now().UTC()).
Where("team_id = ?", teamID).
Join("JOIN students ON students.id = student_id").
Where("students.user_id IN(?)", pg.In(ids)).
Select()
if err != nil {
return nil, err
}
// 你的其他代码
}
了解更多:
英文:
Querying inside a loop is not always a good idea. You can use IN
clause to check the ids
are exists in the table.
You can use Column
method to select some specific columns
func (q *Query) Column(columns ...string) *Query {
.....
}
You can rewrite your method as follows
func (p *storage) FindStudentsInTeam(ids []int, teamID int) (map[int]bool, error) {
// rest of your codes
var existedStudentTeams []Team
err := p.Model(&existedStudentTeams).
Column("team_id").
Where("create_date <= ?", time.Now().UTC()).
Where("team_id = ?", teamID).
Join("JOIN students ON students.id = student_id").
Where("students.user_id IN(?)", pg.In(ids)).
Select()
if err != nil {
return nil, err
}
// rest of your codes
}
See More about
答案2
得分: 0
尝试这个
func (p *storage) FindStudentsInTeam(ids []int, teamID int) (map[int]bool, error) {
studentsInTeam := make(map[int]bool, len(ids))
// 准备查询
query := p.Model((*Team)(nil)).
Select("students.user_id").
Where("create_date <= ?", time.Now().UTC()).
Where("team_id = ?", teamID).
Where("students.user_id IN (?)", ids)
// 执行查询
var result []StudentInTeam
if err := query.Find(&result).Error; err != nil {
return nil, err
}
// 构建结果映射
for _, student := range result {
studentsInTeam[student.UserID] = true
}
return studentsInTeam, nil
}
英文:
try this
//
func (p *storage) FindStudentsInTeam(ids []int, teamID int) (map[int]bool, error) {
studentsInTeam := make(map[int]bool, len(ids))
// Prepare the query
query := p.Model((*Team)(nil)).
Select("students.user_id").
Where("create_date <= ?", time.Now().UTC()).
Where("team_id = ?", teamID).
Where("students.user_id IN (?)", ids)
// Execute the query
var result []StudentInTeam
if err := query.Find(&result).Error; err != nil {
return nil, err
}
// Build the result map
for _, student := range result {
studentsInTeam[student.UserID] = true
}
return studentsInTeam, nil
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论