duangong1979 2018-04-05 14:58
浏览 204
已采纳

多个row.Next()同时进入golang sql / database

I am working on a function which getting users my problem is i have to return the result of the two queries as one result in the rows.next() and rows1.next() like user_id,subject,phone and also body as same result .

here is what i made but it doesn't work cloud you please help me out with some suggestions , thanks indeed func GetUsers() (users []Users, err error) {

users = make([]Users, 0)
rows, err := db1.Query("SELECT  user_id, subject,phone FROM users limit 11")
rows1, err := db1.Query("Select body from users limit 11")
defer rows.Close()
if err != nil {
    // handle this error better than this
    log.Fatal(err)
    //return
}
var user Users
for rows.Next() {
    rows.Scan(&user.ID, &user.Subject, &user.Phone)
    users = append(users, user)
}
for rows1.Next() {

    rows1.Scan(&user.Body)
    users = append(users, user)

}
return 
}

i have a table called users which has no primary key , basically user_id . name. body 1 . model . 2 1 . brand. 1 1 . fuel. 3 1 . date .1 1 . year . 1

i have to have a result userid , model, brand , fuel ,date,year 1 . 2 1 . 3 . 1 . 1

so the values in the column name gonna be as a rows i have already defined my struct which containning all the fields needed

  • 写回答

1条回答 默认 最新

  • duan0424 2018-04-05 15:21
    关注

    Looking at the example code it's clear that you should do whatever you're doing in one query instead of two. Even if your real code is a little different, say you need to query more than one table, you should still probably do only one query using JOINs.

    users := make([]*User, 0)
    rows, err := db1.Query("SELECT  user_id, subject,phone, body FROM users limit 11")
    if err != nil {
        log.Fatal(err)
    }
    defer rows.Close()
    
    for rows.Next() {
        user := new(User)
        if err := rows.Scan(&user.ID, &user.Subject, &user.Phone, &user.Body); err != nil {
            panic(err)
        }       
        users = append(users, user)
    }
    if err := rows.Err(); err != nil {
        panic(err)
    }
    

    That said, if you want to do it the way you illustrated in your question you can do that like this.

    (this probably not what you should do)

    users := make([]*User, 0)
    rows, err := db1.Query("SELECT  user_id, subject,phone FROM users limit 11")
    if err != nil {
        log.Fatal(err)
    }
    defer rows.Close()
    
    rows1, err := db1.Query("Select body from users limit 11")
    if err != nil {
        panic(err)
    }
    defer rows1.Close()
    
    for rows.Next() {
        user := new(User)
        if err := rows.Scan(&user.ID, &user.Subject, &user.Phone); err != nil {
            panic(err)
        }
        if !rows1.Next() {
            panic("no next body row")
        }
        if err := rows1.Scan(&user.Body); err != nil {
            panic(err)
        }
    
        users = append(users, user)
    }
    if err := rows.Err(); err != nil {
        panic(err)
    }
    if err := rows1.Err(); err != nil {
        panic(err)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python实现网页视频下载
  • ¥15 如何通过深度图获得物体的完整、正确点云?
  • ¥15 找一本C#4.5参考大全电子书
  • ¥15 有没有操作系统适用白丁的经典的书籍,如何解决?(标签-学习)
  • ¥15 Catia V5 R20 64位 安装过程中选择orbix配置创建套接字失败
  • ¥100 C51单片机设计交通灯时出现的问题
  • ¥15 R语言爬虫的时候元素和园代码不一样怎么解决呀
  • ¥15 VS2022多项目启动有问题
  • ¥15 SQL删除添加数据后序号不连续问题。
  • ¥15 首次运行OmniEvent运行报错