首页 > 其他分享 >使用gorm进行数量统计【limit、offset对count的统计的影响】

使用gorm进行数量统计【limit、offset对count的统计的影响】

时间:2023-06-07 16:24:46浏览次数:48  
标签:count Count global Limit offset total gorm

limit、offset对count的统计的影响

错误示例1:请注意,如下例子中,Count放在了最后面,查询时,count方法也会加上Limit和offset这两个语句:

global.DB.Limit(10).Offset(2).Find(&users).Count(&total)

错误示例2:
下面这种方法,看似没啥问题,实际上count的时候也会带上分页。

var orm=global.DB.Where("id =1")
orm.Limit(10).Offset(2).Find(&users)
orm.Count(&total)

正确用法是,先count,然后再分页和find

global.DB.Count(&total).Limit(10).Offset(2).Find(&users)

 

标签:count,Count,global,Limit,offset,total,gorm
From: https://www.cnblogs.com/beatle-go/p/17463713.html

相关文章

  • Luogu P3605 [USACO17JAN]Promotion Counting P
    [USACO17JAN]PromotionCountingP题目描述Thecowshaveonceagaintriedtoformastartupcompany,failingtorememberfrompastexperiencethatcowsmaketerriblemanagers!Thecows,convenientlynumbered\(1\ldotsN\)(\(1\leqN\leq100,000\)),o......
  • Gorm - sql查询某个字段为空时赋默认值
    场景:查询时报错err{"error":"sql:Scanerroroncolumnindex0,name\"mobile\":convertingNULLtostringisunsupported"}总结:根据项目需要,在数据库查询时,可能需要对查询结果进行封装,为避免前端抛出空指针异常(NullPointException),对于没有值的字段也不能......
  • GORM支持的全部标签
    column:指定数据库中的字段名,例如:column:name。type:指定数据库中的字段类型,例如:type:varchar(255)。size:指定字段的大小,例如:size:255。primaryKey:指定该字段为主键,例如:primaryKey。autoIncrement:指定该字段自增,例如:autoIncrement。default:指定该字段的默认值,例如:defaul......
  • CF1838E - Count Supersequences
    先考虑正着做,我们只考虑整个\(b\)序列中出现的第一个子序列\(a\)。这样,我们就是要往\(a\)中插入\(m-n\)个数,其中\(a_{i-1}\)和\(a_i\)之间不能有\(a_i\)(否则就会有更靠前的子序列)。\(a_1\)前面不能有\(a_1\),\(a_n\)后面什么都可以有。我们发现,我们可以先枚举\(a......
  • python requests请post接口200,打印提示Unexpected character encountered while parsi
    pythonrequests发起httppost请求,带参数,带请求头,代码设置检查没有问题runpy文件提示Unexpectedcharacterencounteredwhileparsingvalue:p.Path,问题一:body请求形式未进行json格式data=json.dumps(body)dumps的功能是将字典类型转换未json格式的字符串类型。......
  • [ABC202E] Count Descendants 题解
    CountDescendants题目大意给定一颗以\(1\)为根的树,多次询问求某点的子树中深度为给定值的点的个数。思路分析对于每个深度开一个vector,从大到小存下这个深度的所有点的dfs序开始值和结束值,询问时只需要在对应深度的vector中二分作差即可。代码#include<iostream>#......
  • gorm 版本对比
    两个版本github.com/jinzhu/gormv1.9.16gorm.io/gormv1.21.3 Open//jinzhufuncOpen(dialectstring,args...interface{})(db*DB,errerror){}//grom.iofuncOpen(dialectorDialector,opts...Option)(db*DB,errerror){} Find//jinzhufunc......
  • Count of Integers
    CountofIntegersYouaregiventwonumericstrings num1 and num2 andtwointegers max_sum and min_sum.Wedenoteaninteger x tobe good if:num1<=x<=num2min_sum<=digit_sum(x)<=max_sum.Returnthenumberofgoodintegers.Sincethe......
  • Flex+J2EE实例(cairngorm+blazeDS+hibernate+spring) part4 (完)
     Flex+J2EE实例(cairngorm+blazeDS+hibernate+spring)part4----addcairngorm1.添加在libs下添加Cairngorm.swc,此时,具备了cairngorm框架能力2.运用cairngorm框架2.1在flex_src下创建如下文件夹和文件   AdminVO.aspackagevo.AdminVO{ [Bindable] publicclassAdm......
  • Flex+J2EE实例(cairngorm+blazeDS+hibernate+spring) part3
    Flex+J2EE实例(cairngorm+blazeDS+hibernate+spring)part3                                   ----addspring &hibernate1.在WEB-INF\lib下添加spring、hibernate常用jar,包括mysql数据库的连接驱动  2.在mysql数据库中创建数据库......