【go】gorm\xorm\ent多表联查

文章目录

  • [1 gorm](#1 gorm)
  • [2 xorm](#2 xorm)
  • [3 ent](#3 ent)

前言:本文介绍golang三种orm框架联表查询

1 gorm

go 复制代码
type UserTest struct {
	Count     int     `json:"count,omitempty"`
	Type      string  `json:"type,omitempty"`
}
res := []UserTest{}
db.Joins("LEFT JOIN user ON order.user_id = user.id").Find(&res) 

2 xorm

go 复制代码
err := db.Table("user").Select("name as org, count(status) as count").
	Join("LEFT", "order", "user.id= order.user_id").
	In("name", orgNames).
	GroupBy("name").
	OrderBy("count(status) desc").Find(&res)

3 ent

go 复制代码
err := db.User.Query().
	Where(user.CompanyIDIn(companyIds...)).
	GroupBy(user.FieldCompanyID).
	Aggregate(
		func(s *sql.Selector) string {
			t := sql.Table(order.Table)
			s.Join(t).On(s.C(user.FieldID), t.C(order.FieldUserID))
			return sql.As(sql.Count(t.C(order.FieldBandwidth)), "count")
		},
	).Scan(c, &res)

输出:

json 复制代码
[
	{
		"type": "xxx",
		"count": 24
	},
	{
		"type": "yyy",
		"count": 65,
	}
]
相关推荐
yue0081 天前
C# 生成指定位数的编号
开发语言·c#
q***57501 天前
Spring Boot(七):Swagger 接口文档
java·spring boot·后端
大笨象、小笨熊1 天前
Qt Widgets和Qt Quick在开发工控触摸程序的选择
开发语言·qt
红黑色的圣西罗1 天前
C# List.Sort方法总结
开发语言·c#
猪猪拆迁队1 天前
前端图形引擎架构设计:双引擎架构设计
前端·后端·架构
E_ICEBLUE1 天前
Python 教程:如何快速在 PDF 中添加水印(文字、图片)
开发语言·python·pdf
南方的狮子先生1 天前
【C++】C++文件读写
java·开发语言·数据结构·c++·算法·1024程序员节
Alex艾力的IT数字空间1 天前
完整事务性能瓶颈分析案例:支付系统事务雪崩优化
开发语言·数据结构·数据库·分布式·算法·中间件·php
GISer_Jing1 天前
Node.js 开发实战:从入门到精通
javascript·后端·node.js