Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
mysql的语法数组_Mysql_Arrays_Postgresql_Go - Fatal编程技术网

mysql的语法数组

mysql的语法数组,mysql,arrays,postgresql,go,Mysql,Arrays,Postgresql,Go,将数据数组添加到数据库的语法是什么, 我发现postgresql的代码是:pg.Array // "ins" is the SQL insert statement ins := "INSERT INTO posts (title, tags) VALUES ($1, $2)" // "tags" is the list of tags, as a string slice tags := []string{"go", "goroutines", "queues"} // the pq.A

将数据数组添加到数据库的语法是什么, 我发现postgresql的代码是:pg.Array

 // "ins" is the SQL insert statement
ins := "INSERT INTO posts (title, tags) VALUES ($1, $2)"

// "tags" is the list of tags, as a string slice
tags := []string{"go", "goroutines", "queues"}

// the pq.Array function is the secret sauce
_, err = db.Exec(ins, "Job Queues in Go", pq.Array(tags))

这里我要打断几点,主要是因为你的问题不够清晰:

弗斯特
pq.Array
用于将数组值转换为PostgreSQL中的安全列表,如以下语句:

db.Query(`SELECT * FROM t WHERE id = ANY($1)`, pq.Array([]int{235, 401}))
其中,生成的查询是:

SELECT * FROM t WHERE id = ANY(235, 401)
这是为了帮助您安全地从列表中创建与类型无关的查询值,这不是您在问题中使用它的方式

第二 如果只是尝试将值封送到数据库列中的逗号分隔列表中,即:

| title   | tags                 |
|---------|----------------------|
| my post | go,goroutines,queues |
SQL驱动程序中不需要特殊的函数。您只需创建值,并让准备好的语句完成它的任务:

tags := []string{"go, goroutines, queues"}
q := "INSERT INTO posts (title, tags) VALUES ($1, $2)"
_, _ = db.Exec(q, "mypost", strings.Join(tags, ","))
第三 您可能会得到更好的服务,使用MySQL中的关系来完成您正在做的事情:

帖子 标签 张贴标签
这将通过不保存重复数据来帮助您节省空间,并且不需要了解数据库中的序列化方法(另外,关系数据库就是这样做的)。然后,当您选择表时,您可以根据需要手工编写
JOIN
语句来检索数据。我强烈建议阅读MySQL中的多对多关系。

这种数据模型过于规范化,难以获得性能(连接比读取字段更昂贵),从而获得相对廉价的磁盘空间。考虑到今天的存储价格,这是一个糟糕的权衡。@MarkusWMahlberg我实际上完全不同意这种说法。现在磁盘很便宜,计算机也很便宜。是的,这会使一个简单的数据结构过于复杂,但我无法想象他只会构建一个只包含这些特性的应用程序。从良好实践开始,最终可以构建更好的应用程序。在前面的方法中,“每个人都使用什么标签”是非常难以回答的。计算和性能之间存在差异。你可以随心所欲地扩展计算——如果一个查询比另一个查询更昂贵,它将花费更长的时间。如果没有合适的用例来证明它的合理性,就没有必要使数据模型复杂化。
| id | title   |
|----|---------|
| 1  | my post |
| id | tag        |
|----|------------|
| 1  | go         |
| 2  | goroutines |
| 3  | queues     |
| posts_id | tags_id |
|----------|---------|
| 1        | 1       |
| 1        | 2       |
| 1        | 3       |