英文:
sqlx structscan join query
问题
我是你的中文翻译助手,以下是你提供的代码的翻译:
我对Go和sqlx都不太熟悉,我想知道是否可以在一个查询中使用结构体扫描(strutscan)来处理主-从连接(master -> detail join)的情况。一个病人有多个病例,所以我想查询所有病人,并在每个病人结构体中嵌入一个Episode数组。
类似这样的代码:
type Episode struct {
EpisodeID int `db:"episode_id"`
PatientID int `db:"patient_id"`
Status string `db:"status"`
}
type Patient struct {
PatientID int `db:"patient_id"`
FirstName string `db:"first_name"`
MiddleName sql.NullString `db:"middle_name"`
LastName string `db:"last_name"`
Episodes []Episode
}
query := `SELECT
mr_patients.patient_id,
mr_patients.first_name,
mr_patients.middle_name,
mr_patients.last_name,
mr_episode_statuses.status,
mr_episodes.episode_id,
mr_episodes.created_at
FROM mr_patients
INNER JOIN mr_episodes ON mr_patients.patient_id = mr_episodes.patient_id
INNER JOIN mr_episode_statuses ON mr_episodes.status_id = mr_episode_statuses.status_id
ORDER BY mr_patients.patient_id, mr_episodes.episode_id`
rows, err := db.Queryx(query)
if err != nil {
log.Fatal(err)
}
var patients []Patient
for rows.Next() {
var p Patient
err = rows.StructScan(&p)
if err != nil {
log.Fatalln(err)
} else {
patients = append(patients, p)
}
}
希望对你有帮助。
英文:
I'm new to go and sqlx and I would want to know if its posible to strutscan a master -> detail join clause in one query. A patient has many episodes so I want to query all patients and embed an Episode array in each Patient struct
Something like this:
type Episode struct {
EpisodeID int `db:"episode_id"`
PatientID int `db:"patient_id"`
Status string `db:"status"`
}
type Patient struct {
PatientID int `db:"patient_id"`
FirstName string `db:"first_name"`
MiddleName sql.NullString `db:"middle_name"`
LastName string `db:"last_name"`
Episodes []Episode
}
query := `SELECT
mr_patients.patient_id,
mr_patients.first_name,
mr_patients.middle_name,
mr_patients.last_name,
mr_episode_statuses.status,
mr_episodes.episode_id,
mr_episodes.created_at
FROM mr_patients
INNER JOIN mr_episodes ON mr_patients.patient_id = mr_episodes.patient_id
INNER JOIN mr_episode_statuses ON mr_episodes.status_id = mr_episode_statuses.status_id
ORDER BY mr_patients.patient_id, mr_episodes.episode_id`
rows, err := db.Queryx(query)
if err != nil {
log.Fatal(err)
}
var patients []Patient
for rows.Next() {
var p Patient
err = rows.StructScan(&p)
if err != nil {
log.Fatalln(err)
} else {
patients = append(patients, p)
}
}
Thanks.
答案1
得分: 2
这不会起作用,因为database/sql
或sqlx
不是对象关系映射器(ORM),它们不知道如何“填充”你的[]Episodes
类型。sqlx
可以使用“structName.fieldName”语法将单个结构字段分配给父结构,但仅限于此。它不能获取所有行,知道哪个是哪个,并将所有的Episodes
正确分配给Patient
。你需要一个ORM来实现这个。
英文:
It's not going to work since database/sql or sqlx aren't object relational mappers and don't know how to "populate" your type []Episodes
. sqlx can assign single struct fields within a parent struct with the "structName.fieldName" syntax but that's it. It can't take all your rows, know what is what and assign all Episodes
to the correct Patient
. You need an ORM for that.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论