猿问

后gresql + 带有组的 gorm 复杂分页

我有带有GroupId列的表,并且可能有大量的行。有没有办法按组对数据进行分页?例如,获得前10组,然后是接下来的10组等?

表将具有此结构(默认情况下不排序) - 抱歉格式不正确,由于某种原因它不起作用,但我认为它是可见的

科尔1科尔2组标识
数据 A傅11
数据 B福21
数据 C傅32

...

那么有没有办法有效地获得拳头10组的行,然后是接下来的10组等,每组的行数不是固定的?假设有数百万行,组可以是任意数字。


肥皂起泡泡
浏览 102回答 1
1回答

料青山看我应如是

它并没有真正分页,而只是通过有界标识符()查询数据。如果您知道组 ID,并且它们是连续的,则可以执行类似操作。这是使用纯Go完成的,您可以轻松地将gorm与此合并。GroupId去游乐场package mainimport (&nbsp; &nbsp; "fmt")func main() {&nbsp; &nbsp; for _, q := range queries() {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(q)&nbsp; &nbsp; }}func queries() (out []string) {&nbsp; &nbsp; groups := []int{10, 20, 30, 40, 50, 60, 70, 80, 90, 100}&nbsp; &nbsp; for i := 0; i < len(groups); i++ {&nbsp; &nbsp; &nbsp; &nbsp; var lower, upper int = 0, 0&nbsp; &nbsp; &nbsp; &nbsp; if i != 0 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lower = groups[i-1]&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; upper = groups[i]&nbsp; &nbsp; &nbsp; &nbsp; q := fmt.Sprintf("SELECT * FROM MyTable WHERE GroupId >= %v AND GroupId < %v", lower, upper)&nbsp; &nbsp; &nbsp; &nbsp; out = append(out, q)&nbsp; &nbsp; }&nbsp; &nbsp; return}对打印出应运行以获取每个组中的所有结果的每个查询的调用。例如:queriesSELECT * FROM MyTable WHERE GroupId >= 0 AND GroupId < 10SELECT * FROM MyTable WHERE GroupId >= 10 AND GroupId < 20SELECT * FROM MyTable WHERE GroupId >= 20 AND GroupId < 30SELECT * FROM MyTable WHERE GroupId >= 30 AND GroupId < 40SELECT * FROM MyTable WHERE GroupId >= 40 AND GroupId < 50SELECT * FROM MyTable WHERE GroupId >= 50 AND GroupId < 60SELECT * FROM MyTable WHERE GroupId >= 60 AND GroupId < 70SELECT * FROM MyTable WHERE GroupId >= 70 AND GroupId < 80SELECT * FROM MyTable WHERE GroupId >= 80 AND GroupId < 90SELECT * FROM MyTable WHERE GroupId >= 90 AND GroupId < 100到目前为止,我们假设您已经确切地知道要查询的“组存储桶”。相反,如果我们假设我们知道有多少个顺序组()以及每个查询想要多少个组(存储桶大小),我们可以轻松创建一个函数来为我们提供应该查询的存储桶。nsfunc groups(n, s int) (out []int) {&nbsp; &nbsp; for i := 0; i <= n; i++ {&nbsp; &nbsp; &nbsp; &nbsp; if i == 0 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if i%s == 0 || i == n {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out = append(out, i)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return}
随时随地看视频慕课网APP

相关分类

Java
我要回答