内爆2个独立的阵列?
我有一个带有查询的脚本,该查询使用联接从两个单独的表中的数据库收集信息。现在我想将这两个数组及其值并排插入到新表中。
$query = "INSERT INTO `new_table`
(column_a, column_b)
VALUES
(implode(something,$array_a), implode(something,$array_b))"
显然语法不正确,但我想知道这是否可能或者我是否走错了路。我尝试了一些查询,但无法让它们排队插入 - 它一次爆炸了它们。
I have a script with a query that gathers information from a db from two separate tables using a join. Now I want to insert those 2 arrays and their values side by side inside a new table.
$query = "INSERT INTO `new_table`
(column_a, column_b)
VALUES
(implode(something,$array_a), implode(something,$array_b))"
Obviously the syntax isn't right, but I was wondering if it was possible or if I'm on the wrong track. I tried a few queries and couldn't get them to line up for an insert - it exploded them one at a time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如您所期望的,这个问题的答案取决于您想要实现的目标。
我假设您的两个数组如下(出于演示目的):
现在,如果您想将这些值放入一个表中(称为 the_table 以便于参考),以便它们像这样创建一行
那么您想要生成的 SQL 是
并且生成该 SQL 的 PHP 可以是
现在,如果您想要生成一组匹配的行,其中两个数组中的每一个都有一对,像这样
SQL 将是
和 PHP 生成那可能是
The answer to this one is, as you would expect, dependent on exactly what you are trying to achieve.
I will assume that your two arrays are as follows (for the purposes of demonstration):
Now, if you are wanting to place these values into a table (called the_table for ease of reference), so that they create a single row like so
Then the SQL you would want to produce is
And the PHP to produce that SQL could be
Now, if you were, instead, wanting to produce a set of matching rows, with one pair from each of the two arrays, like so
The SQL would be
And the PHP to produce that could be
使用:
也就是说,存储非规范化数据通常不是一个好主意。
Use:
That said, typically not a great idea to be storing denormalized data.