有如下两张表
```go
type Activity_record struct {
BaseModel
Activity_config_id int `json:"activity_config_id"`
Activity_config Activity_config `json:"ac" gorm:"goreignkey:Activity_config_id"`
Share_user_id int `json:"share_user_id"`
Receive_user_id int `json:"receive_user_id"`
}
type Activity_config struct {
BaseModel
Mgr_user_id int `json:"mgr_user_id"`
Type int8 `json:"type"`
User_restrict int8 `json:"user_restrict"`
Title string `json:"title"`
}
```
查询语句,用的gorm:
```go
model.DB.Preload("Activity_config", func(query *gorm.DB) *gorm.DB {
return query.Select("id, type")
}).Select("activity_records.id,activity_records.activity_config_id").Find(&list)
```
打印的sql语句:
[2020-07-06 09:54:57] [1.00ms] SELECT activity_records.id,activity_records.activity_config_id FROM `activity_records`
[2020-07-06 09:54:57] [0.99ms] SELECT id, type FROM `activity_configs` WHERE (`id` IN (35))
返回数据:
[
{
"id": 4,
"activity_config_id": 35,
"ac": {
"id": 35,
"type": 1,
"title ": ""
},
"share_user_id ": 0,
"receive_user_id ": 0
}
]
如何将返回数据中的无用字段方便去掉呢?(如:title,share_user_id,receive_user_id)
但是看起来它执行了两次查询,又没有别的什么方式让生成如下的sql语句,并且返回数据结构如上面的结构:如
```sql
select ar.*, ac.* from activity_records ar inner join activity_configs ac on ac.id = ar.Activity_config_id
```
刚开始看golang,大佬些指点下
有疑问加站长微信联系(非本文作者)