代码之家  ›  专栏  ›  技术社区  ›  faressoft

我可以一次在MySQL中插入2行或数组吗

  •  2
  • faressoft  · 技术社区  · 15 年前

    我可以一次在MySQL中插入2行或数组吗?

    4 回复  |  直到 15 年前
        1
  •  4
  •   nerkn    15 年前
    insert into table (name,other) values ('name1', 'other2'), ('name2', 'other2'), ('name3', 'other3') 
    

    这意味着你可以用前臂

     $values = Array();
     foreach($array as $arr)
       $values[] = "('{$arr['name']}','{$arr['name']}')";
     query("insert into table values" . implode(', ', $values ));
    
        2
  •  3
  •   Alex    15 年前

    是的,当然。

    http://dev.mysql.com/doc/refman/5.1/en/insert.html

    INSERT INTO my_table (field1,field2) VALUES (value1, value2), (value3, value4)
    

    另外一种方法不仅适用于MySQL

    INSERT INTO my_table (field1,field2)
    SELECT value1, value2
    UNION ALL SELECT value3, value4
    UNION ALL SELECT value5, value6
    
        3
  •  2
  •   Simon Quentin    15 年前
    $sql = "INSERT INTO table_name (title) VALUES ('row 1'), ('row 2'), ('row 3')";
    

    http://dev.mysql.com/doc/refman/5.1/en/insert.html

        4
  •  1
  •   Mark Baijens    15 年前

    应使用多个值。

    INSER INTO [table] ([columns]) VALUES ([values],[values]);
    
    推荐文章