1 回答

TA貢獻(xiàn)1772條經(jīng)驗(yàn) 獲得超8個贊
它并沒有真正分頁,而只是通過有界標(biāo)識符()查詢數(shù)據(jù)。如果您知道組 ID,并且它們是連續(xù)的,則可以執(zhí)行類似操作。這是使用純Go完成的,您可以輕松地將gorm與此合并。GroupId
package main
import (
"fmt"
)
func main() {
for _, q := range queries() {
fmt.Println(q)
}
}
func queries() (out []string) {
groups := []int{10, 20, 30, 40, 50, 60, 70, 80, 90, 100}
for i := 0; i < len(groups); i++ {
var lower, upper int = 0, 0
if i != 0 {
lower = groups[i-1]
}
upper = groups[i]
q := fmt.Sprintf("SELECT * FROM MyTable WHERE GroupId >= %v AND GroupId < %v", lower, upper)
out = append(out, q)
}
return
}
對打印出應(yīng)運(yùn)行以獲取每個組中的所有結(jié)果的每個查詢的調(diào)用。例如:queries
SELECT * FROM MyTable WHERE GroupId >= 0 AND GroupId < 10
SELECT * FROM MyTable WHERE GroupId >= 10 AND GroupId < 20
SELECT * FROM MyTable WHERE GroupId >= 20 AND GroupId < 30
SELECT * FROM MyTable WHERE GroupId >= 30 AND GroupId < 40
SELECT * FROM MyTable WHERE GroupId >= 40 AND GroupId < 50
SELECT * FROM MyTable WHERE GroupId >= 50 AND GroupId < 60
SELECT * FROM MyTable WHERE GroupId >= 60 AND GroupId < 70
SELECT * FROM MyTable WHERE GroupId >= 70 AND GroupId < 80
SELECT * FROM MyTable WHERE GroupId >= 80 AND GroupId < 90
SELECT * FROM MyTable WHERE GroupId >= 90 AND GroupId < 100
到目前為止,我們假設(shè)您已經(jīng)確切地知道要查詢的“組存儲桶”。相反,如果我們假設(shè)我們知道有多少個順序組()以及每個查詢想要多少個組(存儲桶大小),我們可以輕松創(chuàng)建一個函數(shù)來為我們提供應(yīng)該查詢的存儲桶。ns
func groups(n, s int) (out []int) {
for i := 0; i <= n; i++ {
if i == 0 {
continue
}
if i%s == 0 || i == n {
out = append(out, i)
}
}
return
}
添加回答
舉報