第三十四:golang 原生 pgsql 对应操作

https://www.lvtao.net/books/golang/go-nested-structure.html

第一:原生查询: Excel 除了查询,其他操作都可以

Go 复制代码
 // 联合查询(left join)
    db.Table("go_service_info").Select("go_service_info.serviceId as service_id, go_service_info.serviceName as service_name, go_system_info.systemId as system_id, go_system_info.systemName as system_name").Joins("left join go_system_info on go_service_info.systemId = go_system_info.systemId").Scan(&results)
    fmt.Println(mapToJson(results))
    // where
    db.Table("go_service_info").Select("go_service_info.serviceId as service_id, go_service_info.serviceName as service_name, go_system_info.systemId as system_id, go_system_info.systemName as system_name").Joins("left join go_system_info on go_service_info.systemId = go_system_info.systemId where go_service_info.serviceId <> ? and go_system_info.systemId = ?", "xxx", "xxx").Scan(&results)
    fmt.Println(mapToJson(results))
    // 原生sql
    db.Raw("SELECT a.serviceId as service_id,a.serviceName as service_name, b.systemId as system_id, b.systemName as system_name FROM go_service_info a LEFT JOIN go_system_info b ON a.systemId = b.systemId").Scan(&results)
    fmt.Println(mapToJson(results))
    // where
    db.Raw("SELECT a.serviceId as service_id,a.serviceName as service_name, b.systemId as system_id, b.systemName as system_name FROM go_service_info a LEFT JOIN go_system_info b ON a.systemId = b.systemId where a.serviceId <> ? and b.systemId = ?", "xxx", "xxx").Scan(&results)
    fmt.Println(mapToJson(results))


原文链接:https://blog.csdn.net/baidu_37366055/article/details/128079638

sql 语句 可以取列表 和总数count

https://2048ai.net/681c1119e47cbf761b68f1cf.html

https://blog.csdn.net/daimading/article/details/85258007?login=from_csdn

用户是一对多的关系,即一个评论属于一个用户,一个用户可以发布多条评论

Go 复制代码
func SelectSingleCommentByCondition(db *gorm.DB, where map[string]interface{}) (Comment, int64, error) {
	var count int64 = 0
	var comment Comment
	err := db.Joins("Author").Where(where).First(&comment).Count(&count).Error
	if count == 0 {
		return comment, 0, errors.New("查询的记录不存在")
	}
	return comment, count, err
}

GORM 更新操作全解 ,子查询,gorm.Expr 执行数据库原生表达式,实现原子操作或复杂计算

Go 复制代码
// 基于struct的批量更新
db.Model(&User{}).Where("role = ?", "admin").Updates(User{Name: "admin_user", Age: 40})
// 生成SQL: UPDATE users SET name='admin_user', age=40 WHERE role = 'admin';

// 基于map的批量更新
db.Table("users").Where("id IN ?", []int{10, 11, 12}).Updates(map[string]interface{}{"status": "inactive", "updated_at": time.Now()})
// 生成SQL: UPDATE users SET status='inactive', updated_at='...' WHERE id IN (10, 11, 12);

// 结合子查询的批量更新
db.Model(&Order{}).Where("amount < (?)", db.Table("orders").Select("AVG(amount)")).Updates(map[string]interface{}{"priority": 2})
// 生成SQL: UPDATE orders SET priority=2 WHERE amount < (SELECT AVG(amount) FROM orders);

作者:Code季风
链接:https://juejin.cn/post/7524864401616601114
来源:稀土掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
相关推荐
不爱说话郭德纲15 小时前
告别漫长的HbuilderX云打包排队!uni-app x 安卓本地打包保姆级教程(附白屏、包体积过大排坑指南)
android·前端·uni-app
Sinclair19 小时前
简单几步,安卓手机秒变服务器,安装 CMS 程序
android·服务器
雮尘1 天前
手把手带你玩转Android gRPC:一篇搞定原理、配置与客户端开发
android·前端·grpc
ktl1 天前
Android 编译加速/优化 80%:一个文件搞定,零侵入零配置
android
alexhilton1 天前
使用FunctionGemma进行设备端函数调用
android·kotlin·android jetpack
冬奇Lab2 天前
InputManagerService:输入事件分发与ANR机制
android·源码阅读
张小潇2 天前
AOSP15 Input专题InputManager源码分析
android·操作系统
RdoZam2 天前
Android-封装基类Activity\Fragment,从0到1记录
android·kotlin
奥陌陌2 天前
android 打印函数调用堆栈
android
用户985120035832 天前
Compose Navigation 3 深度解析(二):基础用法
android·android jetpack