dongzhen7108 2014-06-25 17:56
浏览 10
已采纳

golang mysql在结构中存储了任意行

so I have sql that looks like this

SELECT accounts.id, accounts.username, accounts.password, 
      accounts.created, accounts.last_logged_in, accounts.access,
      banned.reason, banned.expires,
      player.x, player.y, player.zone
FROM accounts
LEFT JOIN banned
ON accounts.id = banned.account_Id
INNER JOIN player
ON accounts.id = player.account_Id
WHERE accounts.username = username

If I wanted to store this in a struct in go I would normally do this:

type Account struct {
    Id           int
    Username     string
    Password     string
    Email        string
    Created      time.Time
    LastLoggedIn time.Time
    AccessLevel  int
    Location     struct {
        Zone string
    }
    Banned []*Banned
}

type Banned struct {
    Reason  string
    Expires time.Time
}
reply := new(Account)

stmt, err := this.Database.Prepare(("CALL findUser(?)"))
    defer stmt.Close()

if err != nil {
    logger.ERROR.Println(err)
    return err
}

err = stmt.QueryRow(args).Scan(&reply.Id, &reply.Username ... you get the idea)

however this is not going work because scan is going to expect a value for every argument and we have left joined onto banned! As the user could have 0 - N bans what’s the best way to tackle this?

Many thanks Zidsal

  • 写回答

1条回答 默认 最新

  • duanbai5348 2014-06-25 19:58
    关注

    I feel that either your example or question isn't what you really wanted to describe, since they don't really ask for the same problem.

    Reading the provided example, you have two different types to scan into (an Account, left join a Banned), that will be repeated for each row of the result. So you just have to create a new Banned struct at the same time that your Account struct and use it to scan the values, then add it to the Account.Banned slice. Loop for each row, and you're done.

    Reading your question, I think that your sql query is somewhat wrong: you have multiples accounts that each have several bans, and you would like to have one account by result row with every ban in it. To do that, you will need to tweak your query with a GROUP BY statement to get one row by account, then the smartest way would be to do a GROUP_CONCAT to get every ban into one attribute that you could then parse accordingly. Example (overly simplified to better expose principle):

      SELECT accounts.id, GROUP_CONCAT(banned.id SEPARATOR ',') as bans
      FROM accounts
      LEFT JOIN banned
      ON accounts.id = banned.account_Id
      WHERE accounts.username = username
      GROUP BY accounts.id
    

    You just have to scan the bans column to a string, split it around ,, etc. In your case, the parsing will be more complex, as you need 2 values in one column, but the principle is the same.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值