Access second row of MySQL query result in Go

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

Access second row of MySQL query result in Go

问题

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

for rows.Next() {

}

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

以下是代码片段:

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

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

英文:

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

for rows.Next {

}

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:

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

答案1

得分: 1

坚持使用DB.Query()

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

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

q := "SELECT status, ts FROM events WHERE node = ? order by ts desc limit 1, 1"
rows, err := db.Query(query, testNode.ID)
if err != nil {
    t.Error("Error:", err)
    return
}
defer rows.Close()

if !rows.Next() {
    t.Error("No result")
    return
}
if err := rows.Scan(&status); err != nil {
    t.Error("Failed to scan:", err)
    return
}

// All good, use status
fmt.Println("Status:", status)

更多使用LIMIT的示例:

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

使用DB.QueryRow()

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

q := "SELECT status, ts FROM events WHERE node = ? order by ts desc limit 1, 1"
if err := db.QueryRow(query, testNode.ID).Scan(&status); err != nil {
    t.Error("Failed to scan, no result?")
    return
}

// All good, use status
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:

q := "SELECT status, ts FROM events WHERE node = ? order by ts desc limit 1, 1"
rows, err := db.Query(query, testNode.ID);
if err != nil {
    t.Error("Error:", err)
    return
}
defer rows.Close()

if !rows.Next() {
    t.Error("No result")
    return
}
if err := rows.Scan(&status); err != nil {
    t.Error("Failed to scan:", err)
    return
}

// All good, use status
fmt.Println("Status:", status)

More examples using LIMIT:

SELECT * FROM tbl LIMIT 5,10;  # Retrieve rows 6-15
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:

q := "SELECT status, ts FROM events WHERE node = ? order by ts desc limit 1, 1"
if err := db.QueryRow(query, testNode.ID).Scan(&status); err != nil {
    t.Error("Failed to scan, no result?")
    return
}

// All good, use status
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:

确定