codecamp

GoFrame 数据查询-Union/UnionAll

GoFrame ORM​ 组件支持​Union/UnionAll​操作,​Union/UnionAll​操作符用于连接两个以上的SELECT语句的结果组合到一个结果集合中,关于​Union/UnionAll​组合查询的相关介绍可以参考​MySQL​的官方文档介绍 https://dev.mysql.com/doc/refman/8.0/en/union.html 。我们可以通过链式操作或者方法操作来实现​Union/UnionAll​操作。

方法定义

// Union does "(SELECT xxx FROM xxx) UNION (SELECT xxx FROM xxx) ..." statement.
func (c *Core) Union(unions ...*Model) *Model

// UnionAll does "(SELECT xxx FROM xxx) UNION ALL (SELECT xxx FROM xxx) ..." statement.
func (c *Core) UnionAll(unions ...*Model) *Model

Union

使用​Union​操作符,多个SELECT语句会删除重复的数据。

// 获取默认配置的数据库对象(配置名称为"default")
db := g.DB()

db.Union(
    db.Model("user").Where("id", 1),
    db.Model("user").Where("id", 2),
    db.Model("user").WhereIn("id", g.Slice{1, 2, 3}),
).OrderDesc("id").All()
// (SELECT * FROM `user` WHERE `id`=1) 
// UNION 
// (SELECT * FROM `user` WHERE `id`=2) 
// UNION 
// (SELECT * FROM `user` WHERE `id` IN (1,2,3) 
// ORDER BY `id` DESC) ORDER BY `id` DESC 

也可以通过​dao​链式操作实现:

dao.User.Union(
    dao.User.Where(dao.User.Columns.Id, 1),
    dao.User.Where(dao.User.Columns.Id, 2),
    dao.User.WhereIn(dao.User.Columns.Id, g.Slice{1, 2, 3}),
).OrderDesc(dao.User.Columns.Id).All()
// (SELECT * FROM `user` WHERE `id`=1) 
// UNION 
// (SELECT * FROM `user` WHERE `id`=2) 
// UNION 
// (SELECT * FROM `user` WHERE `id` IN (1,2,3) 
// ORDER BY `id` DESC) ORDER BY `id` DESC 

UnionAll

使用​UnionAll​操作符,多个SELECT​语句不会删除重复的数据。

db.UnionAll(
    db.Model("user").Where("id", 1),
    db.Model("user").Where("id", 2),
    db.Model(table).WhereIn("id", g.Slice{1, 2, 3}),
).OrderDesc("id").All()
// (SELECT * FROM `user` WHERE `id`=1) 
// UNION ALL 
// (SELECT * FROM `user` WHERE `id`=2) 
// UNION ALL 
// (SELECT * FROM `user` WHERE `id` IN (1,2,3) 
// ORDER BY `id` DESC) ORDER BY `id` DESC 

也可以通过​dao​链式操作实现:

dao.User.UnionAll(
    dao.User.Where(dao.User.Columns.Id, 1),
    dao.User.Where(dao.User.Columns.Id, 2),
    dao.User.WhereIn(dao.User.Columns.Id, g.Slice{1, 2, 3}),
).OrderDesc(dao.User.Columns.Id).All()
// (SELECT * FROM `user` WHERE `id`=1) 
// UNION ALL 
// (SELECT * FROM `user` WHERE `id`=2) 
// UNION ALL 
// (SELECT * FROM `user` WHERE `id` IN (1,2,3) 
// ORDER BY `id` DESC) ORDER BY `id` DESC 


GoFrame 数据查询-Group/Order/Having
GoFrame 数据查询-子查询特性
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

GoFrame 核心组件

GoFrame 核心组件-数据库ORM

GoFrame 模块列表

GoFrame 模块列表-单元测试

GoFrame 模块列表-功能调试

GoFrame WEB服务开发

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }