第三十四: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
来源:稀土掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
相关推荐
aini_lovee9 分钟前
MATLAB基于小波技术的图像融合实现
开发语言·人工智能·matlab
R1nG86322 分钟前
多线程安全设计 CANN Runtime关键数据结构的锁优化
开发语言·cann
初次见面我叫泰隆22 分钟前
Qt——5、Qt系统相关
开发语言·qt·客户端开发
亓才孓27 分钟前
[Class的应用]获取类的信息
java·开发语言
开开心心就好35 分钟前
AI人声伴奏分离工具,离线提取伴奏K歌用
java·linux·开发语言·网络·人工智能·电脑·blender
Never_Satisfied39 分钟前
在JavaScript / HTML中,关于querySelectorAll方法
开发语言·javascript·html
3GPP仿真实验室1 小时前
【Matlab源码】6G候选波形:OFDM-IM 增强仿真平台 DM、CI
开发语言·matlab·ci/cd
devmoon1 小时前
在 Polkadot 上部署独立区块链Paseo 测试网实战部署指南
开发语言·安全·区块链·polkadot·erc-20·测试网·独立链
lili-felicity1 小时前
CANN流水线并行推理与资源调度优化
开发语言·人工智能
沐知全栈开发1 小时前
CSS3 边框:全面解析与实战技巧
开发语言