jmoiron / sqlx

general purpose extensions to golang's database/sql
http://jmoiron.github.io/sqlx/
MIT License
16.28k stars 1.09k forks source link

how to inner join with sqlx? #664

Open aditya37 opened 4 years ago

aditya37 commented 4 years ago

hay sir. i have struct like this type EmployeAccount struct { Id int64json:"idEmploye" Username stringjson:"username" Password stringjson:"password" Email stringjson:"email" PhotoProfile stringdb:"photo_profile" json:"photo_profile" RefreshToken stringdb:"refresh_token" json:"refresh_token" IsActive stringdb:"is_active" json:"isActive" DateCreate time.Timedb:"date_create" json:"date_create" DateUpdate time.Timedb:"date_update" json:"date_update" EmployeEducations []EmployeEducationjson:"employe_education" }

type EmployeEducation struct { InstitutionName stringdb:"institution_name" json:"institution_name" Degree stringdb:"degree" json:"degree" Certificate stringdb:"certificate" json:"certificate_link" IsActive stringdb:"is_active" json:"IsActive" StartEducation time.Timedb:"start_education" json:"start_education" EndEducation time.Timedb:"end_education" json:"end_education" EmployeId int64db:"employe_id" json:"-" } and i want create result with inner join like this { "status": 1, "message": "Success Load data", "Result": [{ "idEmploye": 1602606154, "username": "aditrah_09", "password": "$2a$10$h/L3M0SbkDqPx8kh9LkNEOZZ93s6L5NelmqAdf8NeLhGOb01.h7Ri", "email": "aditrahman909@gmail.com", "photo_profile": "https://storage.googleapis.com/download/storage/v1/b/backend-jobs-go.appspot.com/o/employe-photo-aditrah_09?generation=1602606154351311&alt=media", "refresh_token": "LpGNiDHyOrVUEgkgnyUm", "isActive": "", "date_create": "2020-10-13T23:22:34.587073+07:00", "date_update": "2020-10-13T23:22:34.587073+07:00", "employe_education": [{ "institution_name": "Polinema", "degree": "D2", "certificate_link": "http://google.com ", "IsActive": "True ", "start_education": "2020-10-26T00:00:00Z", "end_education": "2020-10-26T00:00:00Z" }, { "institution_name": "SMKN 3 Bojonegoro", "degree": "SMK", "certificate_link": "http://google.com ", "IsActive": "True ", "start_education": "2020-10-26T00:00:00Z", "end_education": "2020-10-26T00:00:00Z" }] }] }

and how to solve this case

cp-sumi-k commented 3 years ago

You can do like this..

var employeAccounts []EmployeAccount
var educations []EmployeEducation

// write mysql's inner join query and bind data in employeAccounts

for _ , item := range employeAccounts {
    educations = append(educations, item.EmployeEducation)
}

employeAccounts[0].EmployeEducations = educations