dtd793353 2017-07-20 13:53
浏览 27
已采纳

使用sqlx进行联接会导致“缺少目标名称”

I have this query that works fine on mysql and return the intended result, but sqlx has difficulty to handle it:

var jokes []model.Joke
err := shared.Dbmap.Select(&jokes, "SELECT * FROM joke INNER JOIN jokevote ON joke.id=jokevote.joke_id AND jokevote.vote=? AND joke.user_id=?", 1, userId)
if err != nil {
    log.Println(err)
}

At runtime, I get no result but this error message in terminal:

missing destination name joke_id

I don't have this issue when querying a single table.

So I'm wondering what is wrong here and how to fix it?

UPDATE: Here are the structs:

type Joke struct {
    ID         int       `db:"id" json:"id"`
    UserID     int       `db:"user_id" json:"user_id"`
    Title      string    `db:"title" json:"title"`
    Content    string    `db:"content" json:"content"`

...
}

type JokeVote struct {
    ID     int `db:"id" json:"id"`
    JokeID int `db:"joke_id" json:"joke_id"`
    UserID int `db:"user_id" json:"user_id"`
    Vote   int `db:"vote" json:"vote"`
}
  • 写回答

1条回答 默认 最新

  • dousendun8411 2017-07-20 14:24
    关注

    When you execute "SELECT * FROM joke INNER JOIN jokevote" you will get columns from both joke and jokevote tables. Try to query "SELECT joke.* FROM joke INNER JOIN jokevote" to get only columns from joke table.

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

报告相同问题?

悬赏问题

  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi