dongtu1789
2017-07-20 04:54
浏览 105

如何使用sqlx进行LEFT JOIN查询?

I'd like to get results from joke table where jokes are upvoated in jokevote table.

Here is the query:

var jokes []model.Joke
err := shared.Dbmap.Select(&jokes, "    SELECT  *
    FROM  joke
    LEFT JOIN  jokevote
    WHERE  joke.user_id=?
      AND  jokevote.user_id=?
      AND  jokevote.vote=1

", userId, userId) if err != nil { fmt.Println("%v ", err)
}

But I get this error:

Error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'WHERE joke.user_id=? AND jokevote.user_id=? AND jokevote.vote=1' at line 1

I have also tried:

err := shared.Dbmap.Select(&jokes, "    SELECT  *
    FROM  joke
    LEFT JOIN  jokevote
    WHERE  joke.user_id=jokevote.user_id
      AND  jokevote.vote=?

", 1)

And got the same error. I looked at the docs and could not find any example of such joins. So wondering how can I fix it.

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题