我见过一个非常相似的问题 here ,但我不确定管道在命令中做了什么,而且它对我不起作用。
所以,这是我试过的代码。
rows, err := db.Query(fmt.Sprintf("SELECT * FROM mytable WHERE mycolumn LIKE 'camel.%'"))
尽管我不知道它是干什么用的,但我也尝试过使用管道。
rows, err := db.Query(fmt.Sprintf("SELECT * FROM mytable WHERE mycolumn LIKE 'camel. || %'"))
因此,它应该做的是匹配该列中以 camel. 开头的任何内容,所以 camel.*
我在这两个例子中得到的错误是
pq: syntax error at or near "("
所以我猜出于某种原因,它作为命令传递的那一行比我想要的要多......也许是引用问题?我尝试了一些其他的东西,但没有任何效果。感谢您的帮助。
最佳答案
func Sprintf(format string, a ...interface{}) string
Sprintf formats according to a format specifier and returns the resulting string.
mt.Println(fmt.Sprintf("SELECT * FROM mytable WHERE mycolumn LIKE 'camel%'"))
//Output:
//SELECT * FROM mytable WHERE mycolumn LIKE 'camel%!'(MISSING)
//Ofc postgres will complain
在这种情况下你不需要fmt.Sprintf
。
rows, err := db.Query("SELECT * FROM mytable WHERE mycolumn LIKE 'camel.%'")
工作正常。
但是如果你真的需要使用 fmt.Sprintf
你必须用 '%%' 转义 '%'
rows, err := db.Query(fmt.Sprintf("SELECT * FROM mytable WHERE mycolumn LIKE 'camel.%%'"))
关于sql - 我在 golang 中的 Postgres 查询有什么问题(LIKE 匹配),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37602726/