内爆2个独立的阵列?

发布于 2024-11-05 18:05:45 字数 318 浏览 0 评论 0原文

我有一个带有查询的脚本,该查询使用联接从两个单独的表中的数据库收集信息。现在我想将这两个数组及其值并排插入到新表中。

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回眸一遍 2024-11-12 18:05:45

正如您所期望的,这个问题的答案取决于您想要实现的目标。

我假设您的两个数组如下(出于演示目的):

$arrayA = array(
  'Alpha' ,
  'Bravo' ,
  'Charlie'
);
$arrayB = array(
  'Zulu' ,
  'Yankee' ,
  'Xray'
);

现在,如果您想将这些值放入一个表中(称为 the_table 以便于参考),以便它们像这样创建一行

column_a              | column_b
"Alpha,Bravo,Charlie" | "Zulu,Yankee,Xray"

那么您想要生成的 SQL 是

INSERT INTO `the_table` ( `column_a` , `column_b` )
VALUES ( "Alpha,Bravo,Charlie" , "Zulu,Yankee,Xray" )

并且生成该 SQL 的 PHP 可以是

$sqlTpl = 'INSERT INTO `the_table` ( `column_a` , `column_b` )
           VALUES ( "%s" , "%s" )';
$sqlStr = sprintf( $sqlTpl ,
            implode( ',' , $arrayA ) ,
            implode( ',' , $arrayB ) );

现在,如果您想要生成一组匹配的行,其中两个数组中的每一个都有一对,像这样

column_a  | column_b
"Alpha"   | "Zulu"
"Bravo"   | "Yankee"
"Charlie" | "Xray"

SQL 将是

INSERT INTO `the_table` ( `column_a` , `column_b` )
VALUES ( "Alpha" , "Zulu" ) ,
       ( "Bravo" , "Yankee" ) ,
       ( "Charlie" , "Xray" )

和 PHP 生成那可能是

$sqlTpl = 'INSERT INTO `the_table` ( `column_a` , `column_b` )
           VALUES ( %s )';
$sqlArr = array();
foreach( $arrayA as $k => $v )
  $sqlArr[] = '"'.$arrayA[$k].'" , "'.$arrayB[$k].'"';
$sqlStr = sprintf( $sqlTpl ,
            implode( ' ) , ( ' , $sqlArr ) );

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):

$arrayA = array(
  'Alpha' ,
  'Bravo' ,
  'Charlie'
);
$arrayB = array(
  'Zulu' ,
  'Yankee' ,
  'Xray'
);

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

column_a              | column_b
"Alpha,Bravo,Charlie" | "Zulu,Yankee,Xray"

Then the SQL you would want to produce is

INSERT INTO `the_table` ( `column_a` , `column_b` )
VALUES ( "Alpha,Bravo,Charlie" , "Zulu,Yankee,Xray" )

And the PHP to produce that SQL could be

$sqlTpl = 'INSERT INTO `the_table` ( `column_a` , `column_b` )
           VALUES ( "%s" , "%s" )';
$sqlStr = sprintf( $sqlTpl ,
            implode( ',' , $arrayA ) ,
            implode( ',' , $arrayB ) );

Now, if you were, instead, wanting to produce a set of matching rows, with one pair from each of the two arrays, like so

column_a  | column_b
"Alpha"   | "Zulu"
"Bravo"   | "Yankee"
"Charlie" | "Xray"

The SQL would be

INSERT INTO `the_table` ( `column_a` , `column_b` )
VALUES ( "Alpha" , "Zulu" ) ,
       ( "Bravo" , "Yankee" ) ,
       ( "Charlie" , "Xray" )

And the PHP to produce that could be

$sqlTpl = 'INSERT INTO `the_table` ( `column_a` , `column_b` )
           VALUES ( %s )';
$sqlArr = array();
foreach( $arrayA as $k => $v )
  $sqlArr[] = '"'.$arrayA[$k].'" , "'.$arrayB[$k].'"';
$sqlStr = sprintf( $sqlTpl ,
            implode( ' ) , ( ' , $sqlArr ) );
憧憬巴黎街头的黎明 2024-11-12 18:05:45

使用:

$query = sprintf("INSERT INTO `new_table` 
                    (column_a, column_b) 
                  VALUES 
                    ('%s', '%s')",
                  implode(something, $array_a),
                  implode(something, $array_b));

也就是说,存储非规范化数据通常不是一个好主意。

Use:

$query = sprintf("INSERT INTO `new_table` 
                    (column_a, column_b) 
                  VALUES 
                    ('%s', '%s')",
                  implode(something, $array_a),
                  implode(something, $array_b));

That said, typically not a great idea to be storing denormalized data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文