Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to implement simple COUNT(*) #47

Closed
owentran opened this issue Feb 27, 2015 · 1 comment
Closed

How to implement simple COUNT(*) #47

owentran opened this issue Feb 27, 2015 · 1 comment

Comments

@owentran
Copy link

The db.Query and db.QueryOne expects a struct to populate. For a simple select count(*) query, what would you recommend?

My attempt:

type QueryResult struct {
    Count int
}

qr := QueryResult{}
_, err := db.QueryOne(&qr, `SELECT COUNT(*) FROM users`)
if err == nil {
   return qr.Count, nil
}
return -1, err

Is there a more idiomatic way to do this? Thanks.

@vmihailenco
Copy link
Member

Check http://godoc.org/gopkg.in/pg.v3#example-LoadInto

var count int
db.QueryOne(pg.LoadInto(&count), `SELECT COUNT(*) FROM users`)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants