gorm union
时间: 2023-11-16 11:54:01 浏览: 244
gorm union 是 GORM ORM 框架中用于实现 SQL UNION 操作的方法。它可以将两个或多个查询的结果集合并成一个结果集并返回。
使用 gorm union 方法的示例代码如下:
```go
db.Table("users").Select("name").Where("age > ?", 30).
Union(db.Table("users").Select("name").Where("age < ?", 20)).
Find(&result)
```
这段代码将查询年龄大于 30 岁的用户姓名,并将其与年龄小于 20 岁的用户姓名合并成一个结果集。
相关问题
GORM SELECT * FROM table1 WHERE type = '图片'UNIONSELECT * FROM table2 WHERE type = '图片'UNIONSELECT * FROM table3 WHERE type = '图片'UNIONSELECT * FROM table4 WHERE type = '图片'UNIONSELECT * FROM table5 WHERE type = '图片';
以上 SQL 语句是一个查询语句,目的是从 5 个表中选出 type 字段为 '图片' 的所有行,并将它们合并成一个结果集。其中使用了 UNION 运算符将多个 SELECT 语句的结果合并。这里的 * 表示选取所有列。GORM 是一个 Go 语言 ORM 框架,可以通过该框架进行数据库操作,包括查询、插入、更新、删除等。在 GORM 中,可以使用 Raw 方法执行原始 SQL 语句,例如:
```
db.Raw("SELECT * FROM table1 WHERE type = ? UNION SELECT * FROM table2 WHERE type = ? UNION SELECT * FROM table3 WHERE type = ? UNION SELECT * FROM table4 WHERE type = ? UNION SELECT * FROM table5 WHERE type = ?", "图片", "图片", "图片", "图片", "图片").Scan(&result)
```
其中,? 是占位符,用于占据 SQL 语句中的参数位置,Scan 方法用于将查询结果转换为指定类型的数据结构。
改进代码func TracingList(ctx *gin.Context) { resp := models.Response{ Code: 0, Msg: "success", } t, _ := strconv.Atoi(ctx.Query("t")) label, _ := strconv.Atoi(ctx.Query("label")) page, _ := strconv.Atoi(ctx.DefaultQuery("page", "1")) pageSize, _ := strconv.Atoi(ctx.DefaultQuery("pageSize", "10")) code := ctx.Query("code") //设施类型 if len(code) == 0 { resp.Code = 400 resp.Msg = "请输入code值" ctx.JSON(400, resp) return } type Total struct { gorm.Model Type int16 Source int16 Explanation string Label int16 FacilityID string } db := common.DB.Session(&gorm.Session{}) if t < 0 || t > 5 { var req interface{} switch t { case 0: req = []*Total{} case 1: req = []*models.Flaw{} case 2: req = []*models.Issue{} case 3: req = []*models.Record{} case 4: req = []*models.Quality{} case 5: req = []*models.Inspection{} } //查询并分页 if t < 1 || t > 5 { res := db.Scopes(models.Paginate(page, pageSize)).Where("facility_id=?", code).Find(&req) if res.RowsAffected == 0 { resp.Code = 400 resp.Msg = "记录不存在" ctx.JSON(400, resp) return } } else { res := db.Scopes(models.Paginate(page, pageSize)).Raw("SELECT * FROM flaws WHERE facility_id = ? "+ "UNION SELECT * FROM issues WHERE facility_id = ? "+ "UNION SELECT * FROM records WHERE facility_id = ? "+ "UNION SELECT * FROM qualities WHERE facility_id = ? "+ "UNION SELECT * FROM inspections WHERE facility_id = ?", "code", "code", "code", "code", "code").Find(&req) if res.RowsAffected == 0 { resp.Code = 400 resp.Msg = "记录不存在" ctx.JSON(400, resp) return } } resp.Data = req ctx.JSON(200, resp) } else if label < 1 || label > 4 { var req []*Total res := db.Scopes(models.Paginate(page, pageSize)).Raw("SELECT * FROM flaws WHERE label = ? "+ "UNION SELECT * FROM issues WHERE label = ? "+ "UNION SELECT * FROM records WHERE label = ? "+ "UNION SELECT * FROM qualities WHERE label = ? "+ "UNION SELECT * FROM inspections WHERE label = ?", "label", "label", "label", "label", "label").Find(&req) if res.RowsAffected == 0 { resp.Code = 400 resp.Msg = "记录不存在" ctx.JSON(400, resp) return } resp.Data = req ctx.JSON(200, resp) } else { ctx.JSON(400, models.Response{ Code: 400, Msg: "参数有误", }) return } }
以下是改进的代码:
```go
func TracingList(ctx *gin.Context) {
resp := models.Response{
Code: 0,
Msg: "success",
}
code := ctx.Query("code")
if len(code) == 0 {
resp.Code = 400
resp.Msg = "请输入code值"
ctx.JSON(400, resp)
return
}
t, err := strconv.Atoi(ctx.Query("t"))
if err != nil || t < 0 || t > 5 {
resp.Code = 400
resp.Msg = "无效的参数t"
ctx.JSON(400, resp)
return
}
label, err := strconv.Atoi(ctx.Query("label"))
if err != nil || label < 1 || label > 4 {
resp.Code = 400
resp.Msg = "无效的参数label"
ctx.JSON(400, resp)
return
}
page, err := strconv.Atoi(ctx.DefaultQuery("page", "1"))
if err != nil || page < 1 {
resp.Code = 400
resp.Msg = "无效的参数page"
ctx.JSON(400, resp)
return
}
pageSize, err := strconv.Atoi(ctx.DefaultQuery("pageSize", "10"))
if err != nil || pageSize < 1 {
resp.Code = 400
resp.Msg = "无效的参数pageSize"
ctx.JSON(400, resp)
return
}
var req interface{}
switch t {
case 0:
req = []*Total{}
case 1:
req = []*models.Flaw{}
case 2:
req = []*models.Issue{}
case 3:
req = []*models.Record{}
case 4:
req = []*models.Quality{}
case 5:
req = []*models.Inspection{}
}
db := common.DB.Session(&gorm.Session{})
res := db.Scopes(models.Paginate(page, pageSize)).Where("facility_id=?", code)
if t >= 1 && t <= 5 {
res = res.Table("flaws").Where("facility_id=?", code).
Or("facility_id=?", code).
Or("facility_id=?", code).
Or("facility_id=?", code).
Or("facility_id=?", code)
}
res.Find(req)
if res.RowsAffected == 0 {
resp.Code = 400
resp.Msg = "记录不存在"
ctx.JSON(400, resp)
return
}
resp.Data = req
ctx.JSON(200, resp)
}
```
主要改进包括:
1. 对输入的参数进行了有效性检查,如 t、label、page 和 pageSize 都需要为正整数,code 不能为空。
2. 使用了 Go 语言内置的错误处理机制,当参数无效时,返回错误信息。
3. 将 SQL 查询语句拆分为两段,根据 t 的值判断使用哪一段,避免了使用 Raw() 方法的安全隐患。
4. 代码结构更加清晰,易于阅读和维护。
阅读全文