如何使用 Gorm 编写此 SQL 查询?

问题描述 投票:0回答:1

我想用 Gorm 编写以下 SQL 查询:

SELECT count(*)
FROM (
      SELECT DISTINCT ON(t1.number) t1.number,
             t1.id
      FROM "tbl_phone_books" AS t1                                                             
      WHERE (customer_id = 100)
) AS phone
LIMIT 5 OFFSET 0;

我写了这段代码,但它不能正常工作:

Db.Debug().
    Model(models.TblPhoneBook{}).
    Select("distinct on(number) number,id).
    Where("customer_id = ? , customerId).
    Offset(offset).
    Limit(5).
    Count(&response)
sql go go-gorm
1个回答
0
投票

db.Table("tbl_phonebooks").Select("distinct on(number) number, id").Where("customer_id = ?", 100).Group("number, id").Limit(5).Offset(0).Count(&count)

© www.soinside.com 2019 - 2024. All rights reserved.