Golang ORDER BY issue with MySql(MySql 的 Golang ORDER BY 问题)
问题描述
我似乎无法使用 db.Select() 动态 ORDER BY.我在谷歌上没有任何运气...
I can't seem to dynamically ORDER BY with db.Select(). I've Googled without any luck...
工作
rows, err := db.Query("SELECT * FROM Apps ORDER BY title DESC")
不起作用
rows, err := db.Query("SELECT * FROM Apps ORDER BY ? DESC", "title")
我没有收到任何错误,查询只是无法订购.
I'm not getting any errors, the query simply fails to order.
推荐答案
Placeholders ('?'
) 只能用于为过滤器参数插入动态的转义值(例如在 WHERE
部分),数据值应出现的位置,而不是 SQL 关键字、标识符等.您不能使用它来动态指定 ORDER BY
OR GROUP BY
值.
Placeholders ('?'
) can only be used to insert dynamic, escaped values for filter parameters (e.g. in the WHERE
part), where data values should appear, not for SQL keywords, identifiers etc. You cannot use it to dynamically specify the ORDER BY
OR GROUP BY
values.
您仍然可以这样做,例如您可以使用 fmt.Sprintf()
像这样组合动态查询文本:
You can still do it though, for example you can use fmt.Sprintf()
to assemble the dynamic query text like this:
ordCol := "title"
qtext := fmt.Sprintf("SELECT * FROM Apps ORDER BY %s DESC", ordCol)
rows, err := db.Query(qtext)
注意事项:
这样做您将不得不手动防御 SQL 注入,例如如果列名的值来自用户,则不能接受任何值而直接将其插入查询中,否则用户将能够做各种坏事.简单地说,您应该只接受英文字母 + 数字 + 下划线 ('_'
) 的字母.
Doing so you will have to manually defend vs SQL injection, e.g. if the value of the column name comes from the user, you cannot accept any value and just insert it directly into the query else the user will be able to do all kinds of bad things. Trivially you should only accept letters of the English alphabet + digits + underscore ('_'
).
无需尝试提供完整、全面的检查器或转义函数,您可以使用这个仅接受英文字母、数字和 '_'
的简单正则表达式:
Without attempting to provide a complete, all-extensive checker or escaping function, you can use this simple regexp which only accepts English letters, digits and '_'
:
valid := regexp.MustCompile("^[A-Za-z0-9_]+$")
if !valid.MatchString(ordCol) {
// invalid column name, do not proceed in order to prevent SQL injection
}
示例(在 Go Playground 上尝试):
Examples (try it on the Go Playground):
fmt.Println(valid.MatchString("title")) // true
fmt.Println(valid.MatchString("another_col_2")) // true
fmt.Println(valid.MatchString("it's a trap!")) // false
fmt.Println(valid.MatchString("(trap)")) // false
fmt.Println(valid.MatchString("also*trap")) // false
这篇关于MySql 的 Golang ORDER BY 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:MySql 的 Golang ORDER BY 问题


- 导入具有可变标题的 Excel 文件 2021-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- SQL 临时表问题 2022-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 更改自动增量起始编号? 2021-01-01