Access second row of MySQL query result in Go

huangapple go评论125阅读模式
英文:

Access second row of MySQL query result in Go

问题

我正在运行一个在Go中的MySQL查询。我想要访问查询结果的第二行。我知道可以使用以下代码:

  1. for rows.Next() {
  2. }

但是我不想为了访问第二行而运行一个循环(然后在迭代第二次后中断循环)。该怎么办?

以下是代码片段:

  1. rows, err := db.Query("SELECT status, ts FROM events WHERE node = ? ORDER BY ts DESC LIMIT 2", testNode.ID)
  2. defer rows.Close()
  3. if err != nil {
  4. t.Error("Some Error" + err.Error())
  5. }
  6. isNext := rows.Next()
  7. if isNext == false {
  8. t.Error("No rows in query result")
  9. }
  10. rows.Scan(&status)
  11. // 如何使用第二行?

请注意,我只会返回翻译好的部分,不会回答关于翻译的问题。

英文:

I am running a MySQL query in Go. I want access the second row of the query result. I know I can use

  1. for rows.Next {
  2. }

But I don't want to run a loop for accessing the second row (and then breaking the loop after it iterates the second time).
What to do?

Here is a code snippet:

  1. rows,err:= db.Query("SELECT status,ts FROM events WHERE node = ? order by ts desc limit 2", testNode.ID);
  2. defer rows.Close()
  3. if ( err!= nil){
  4. t.Error("Some Error" + err.Error())
  5. }
  6. isNext:=rows.Next()
  7. if(isNext == false) {
  8. t.Error(" No rows in query result")
  9. }
  10. rows.Scan(&status)
  11. // What to do to use second row ?

答案1

得分: 1

坚持使用DB.Query()

如果你要丢弃第一行,那么从数据库中检索它就没有意义。

使用LIMIT 1, 1来丢弃第一个结果并将结果限制为1行(请查看LIMIT的文档:MySQL SELECT语法)。然后简单地继续读取第一行,它将是查询结果的第二行:

  1. q := "SELECT status, ts FROM events WHERE node = ? order by ts desc limit 1, 1"
  2. rows, err := db.Query(query, testNode.ID)
  3. if err != nil {
  4. t.Error("Error:", err)
  5. return
  6. }
  7. defer rows.Close()
  8. if !rows.Next() {
  9. t.Error("No result")
  10. return
  11. }
  12. if err := rows.Scan(&status); err != nil {
  13. t.Error("Failed to scan:", err)
  14. return
  15. }
  16. // All good, use status
  17. fmt.Println("Status:", status)

更多使用LIMIT的示例:

  1. SELECT * FROM tbl LIMIT 5,10; # 检索第6-15
  2. SELECT * FROM tbl LIMIT 5; # 检索前5

使用DB.QueryRow()

如果你预期最多只有1行结果,你也可以使用DB.QueryRow(),结果会更加简洁:

  1. q := "SELECT status, ts FROM events WHERE node = ? order by ts desc limit 1, 1"
  2. if err := db.QueryRow(query, testNode.ID).Scan(&status); err != nil {
  3. t.Error("Failed to scan, no result?")
  4. return
  5. }
  6. // All good, use status
  7. fmt.Println("Status:", status)
英文:

Sticking to DB.Query()

If you're going to discard the first row, there is no point in retrieving it from the database.

Use LIMIT 1, 1 to discard the first result and limit the result to 1 row (check out the doc of LIMIT at: MySQL SELECT syntax). Then simply proceed reading the first row which will be the 2nd row of your query result:

  1. q := "SELECT status, ts FROM events WHERE node = ? order by ts desc limit 1, 1"
  2. rows, err := db.Query(query, testNode.ID);
  3. if err != nil {
  4. t.Error("Error:", err)
  5. return
  6. }
  7. defer rows.Close()
  8. if !rows.Next() {
  9. t.Error("No result")
  10. return
  11. }
  12. if err := rows.Scan(&status); err != nil {
  13. t.Error("Failed to scan:", err)
  14. return
  15. }
  16. // All good, use status
  17. fmt.Println("Status:", status)

More examples using LIMIT:

  1. SELECT * FROM tbl LIMIT 5,10; # Retrieve rows 6-15
  2. SELECT * FROM tbl LIMIT 5; # Retrieve first 5 rows

Using to DB.QueryRow()

If you're expecting at most 1 row, you may also use DB.QueryRow() and the result will be much more compact:

  1. q := "SELECT status, ts FROM events WHERE node = ? order by ts desc limit 1, 1"
  2. if err := db.QueryRow(query, testNode.ID).Scan(&status); err != nil {
  3. t.Error("Failed to scan, no result?")
  4. return
  5. }
  6. // All good, use status
  7. fmt.Println("Status:", status)

huangapple
  • 本文由 发表于 2017年2月2日 16:25:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/41997341.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定