为了账号安全,请及时绑定邮箱和手机立即绑定

如何显示查询结果计数

如何显示查询结果计数

Go
暮色呼如 2023-06-05 19:34:02
我正在做一个 RESTful API 项目,我的代码可以用 gorm 查询,我的查询是这样的countSequenceId := db.Debug().Raw("SELECT COUNT (*) FROM SMSBlast2").Scan(&smsblast1)。我得到了结果[1 rows affected or returned],这意味着成功地计算了我在数据库中的所有行,但我想显示结果result count = 10,但是如何?package mainimport (    "encoding/json"    "fmt"    "github.com/gorilla/mux"    "github.com/jinzhu/gorm"    _ "github.com/jinzhu/gorm/dialects/mssql"    "log"    "net/http"    "strconv"    "time")type SMSBlast struct {    SequenceID   int `gorm:"primary_key";column:"SequenceID"`    MobilePhone string `gorm:"column:MobilePhone"`    Output  string  `gorm:"column:Output"`    WillBeSentDate *time.Time `gorm:"column:WillBeSentDate"`    SentDate *time.Time `gorm:"column:SentDate"`    Status *string `gorm:"column:Status"`    DtmUpd time.Time `gorm:"column:DtmUpd"`}func (SMSBlast) TableName() string {    return "SMSBlast2"}func allSMSBlasts(w http.ResponseWriter, r *http.Request){    db, err := gorm.Open("mssql", "sqlserver://sa:@localhost:1433?database=CONFINS")    if err != nil{        panic("failed to connect database")    }    defer db.Close()    var smsblasts []SMSBlast    db.Debug().Find(&smsblasts)    fmt.Println("{}",smsblasts)    json.NewEncoder(w).Encode(smsblasts)}func insertSMSBlast(w http.ResponseWriter, r *http.Request){    fmt.Println("New Insert Created")    db, err := gorm.Open("mssql", "sqlserver://sa:@localhost:1433?database=CONFINS")    if err != nil{        panic("failed to connect database")    }    defer db.Close()    vars := mux.Vars(r)    mobilephone := vars["mobilephone"]    output := vars["output"]    var(        smsblast1 SMSBlast    )    countSequenceId := db.Debug().Raw("SELECT COUNT (*) FROM SMSBlast2").Scan(&smsblast1)    fmt.Println(countSequenceId)    msg, err :=  json.Marshal(countSequenceId)    if err != nil{        fmt.Println(err.Error())    }
查看完整描述

1 回答

?
侃侃无极

TA贡献2051条经验 获得超10个赞

我不确定你为什么为此使用 Raw 方法,但我想指出有一个 Count 方法可以实现你想要的:

db.Where("name = ?", "jinzhu").Or("name = ?", "jinzhu 2").Find(&users).Count(&count)

//// SELECT * from USERS WHERE name = 'jinzhu' OR name = 'jinzhu 2'; (users)

//// SELECT count(*) FROM users WHERE name = 'jinzhu' OR name = 'jinzhu 2'; (count)


db.Model(&User{}).Where("name = ?", "jinzhu").Count(&count)

//// SELECT count(*) FROM users WHERE name = 'jinzhu'; (count)

我从文档中整理了一个非常简单的示例:


package main


import (

    "fmt"


    "github.com/jinzhu/gorm"

    _ "github.com/jinzhu/gorm/dialects/sqlite"

)


type Porg struct {

    gorm.Model

    Name string

}


func main() {

    db, err := gorm.Open("sqlite3", "test.db")

    if err != nil {

        panic("failed to connect database")

    }

    defer db.Close()


    // Migrate the schema

    db.AutoMigrate(&Porg{})


    // Create

    for i := 1; i <= 100; i++ {

        db.Create(&Porg{Name: "John"})

    }


    // Read

    var porgs []Porg

    var count int

    db.Model(&porgs).Count(&count)


    fmt.Println(count)

}

输出:100


使用Model您可以指定要查询的模型的方法,这不会直接查询数据库。使用db.Find(&porgs).Count(&count)实际上会向数据库发送 2 个 SQL 查询。


查看完整回答
反对 回复 2023-06-05
  • 1 回答
  • 0 关注
  • 87 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信