若是一次插入多行,数据库查询会更快吗: php
喜欢 html
INSERT.... UNION INSERT.... UNION
(我须要插入2-3000行) mysql
BEGIN; INSERT INTO test_b (price_sum) SELECT price FROM test_a; INSERT INTO test_c (price_summ) SELECT price FROM test_a; COMMIT;
// db table name / blog_post / menu / site_title // Insert into Table (column names separated with comma) $sql = "INSERT INTO product_cate (site_title, sub_title) VALUES ('$site_title', '$sub_title')"; // db table name / blog_post / menu / site_title // Insert into Table (column names separated with comma) $sql = "INSERT INTO menu (menu_title, sub_menu) VALUES ('$menu_title', '$sub_menu', )"; // db table name / blog_post / menu / site_title // Insert into Table (column names separated with comma) $sql = "INSERT INTO blog_post (post_title, post_des, post_img) VALUES ('$post_title ', '$post_des', '$post_img')";
这是一个可与an:m(多对多关系)表一块儿使用的PHP解决方案: sql
// get data $table_1 = get_table_1_rows(); $table_2_fk_id = 123; // prepare first part of the query (before values) $query = "INSERT INTO `table` ( `table_1_fk_id`, `table_2_fk_id`, `insert_date` ) VALUES "; //loop the table 1 to get all foreign keys and put it in array foreach($table_1 as $row) { $query_values[] = "(".$row["table_1_pk_id"].", $table_2_fk_id, NOW())"; } // Implode the query values array with a coma and execute the query. $db->query($query . implode(',',$query_values));
使用
VALUES
语法的INSERT
语句能够插入多行。 为此,请包括多个列值列表,每一个列值括在括号内并用逗号分隔。 数据库
例: ide
INSERT INTO tbl_name (a,b,c) VALUES (1,2,3), (4,5,6), (7,8,9);
资源 oop
若是您的数据在文本文件中,则可使用LOAD DATA INFILE 。 post
从文本文件加载表时,请使用LOAD DATA INFILE。 这一般比使用INSERT语句快20倍。 优化
优化INSERT语句 spa
您能够在上面的连接中找到有关如何加快插入语句的更多提示。