2

I have an array of 1500000 records in it as below

Array = ["2","3","6","7","A5057",......]

How would I be able to insert all this records in a table(which has only one field(XXX_id)) directly from the MySQL command I tried with the below query

INSERT INTO TABLE_NAME (XXX_id) VALUES (["2","3","6","7","A5057",......]);

If we have to insert from php script, no doubt we can follow this question form community.

2
  • you will need a procedure for what you want to to Commented Oct 26, 2017 at 5:46
  • use the stored procedure,first split the array into rows and store them into temporary table .Then after insert data from temporary table to physical table Commented Oct 26, 2017 at 6:03

8 Answers 8

2

You can create array format like this

INSERT INTO `TABLE_NAME`(`XXX_id`) VALUES (1),(2),(3),(4)

Read more

Sign up to request clarification or add additional context in comments.

Comments

1

There are actually two ways you can do this

1.LOAD INFILE

LOAD DATA LOCAL INFILE  
'path/file.csv'
INTO TABLE giata_table 
FIELDS TERMINATED BY ',' 
ENCLOSED BY '"'
LINES TERMINATED BY '\n'

2.Use multi-row insert

INSERT INTO `TABLE_NAME`(`XXX_id`) VALUES (1),(2),(3),(4)

Comments

1

You can use multi-row insert to achieve what you want

Just format your array as VALUES (X), (X), ... with PHP

2 Comments

I think trying to do that for 1.5 million rows will exceed the max_packet_size limit.
This is MySQL server dependant from the Op (and changeable anyway) but it's still the optimized way for very large insert
1

Put the array data into a file, with one ID per line. Then you can use LOAD DATA INFILE:

LOAD DATA INFILE 'filename'
INTO TABLE table_name (XXX_id)

8 Comments

I am trying to insert the records from phpmyadmin.
You tagged it php.
There are no arrays in phpMyAdmin. It's a web interface, not a programming language. So now your question makes no sense.
My mistake, I changed the tags.
Yes you are correct I can't use array but can change my array to the format as @Plotisateur mentioned.
|
1

Your Query should be like this:

INSERT INTO TABLE_NAME (XXX_id) VALUES (2),(3),(6),(7),(A5057),......;

But you are going to insert larege amount of rows with just one Query. So, it may exceeds to MySQL query limitation. Every query is limited by max_allowed_packet in general for MySQL.

1) Execute the following command in in MySQL to view default value for 'max_allowed_packet ':

show variables like 'max_allowed_packet';

2) Standard MySQL installation has a default value of 1048576 bytes (1MB). But this can be increased by setting the higher value to 500MB or may be more:

SET GLOBAL max_allowed_packet=524288000;

3) Check max_allowed_packet value again by following 1) command in MySQL.

Hope this helps you.

Comments

1

we can use LOAD DATA LOCAL INFILE

LOAD DATA LOCAL INFILE  
'path/file.csv'
INTO TABLE giata_table 
FIELDS TERMINATED BY ',' 
ENCLOSED BY '"'
LINES TERMINATED BY '\n'

This will fetch all the records in a table we have to make sure that the field name in the table and the heading in the csv file matches the same.

If this didn't works for you then you can follow the @Plotisateur and @Aiyaz khorajia answer as

INSERT INTO `TABLE_NAME`(`XXX_id`) VALUES (1),(2),(3),(4)

Comments

0
$string =  ' (" '.implode(' "),(" ',$array).' ") ';
$query = "INSERT INTO TABLE_NAME (XXX_id) VALUES {$string}";

IF you have large number of records, I would suggest you to make a bunch of 100-200 records and then insert:

for($i=0; $i<sizeof($array); $i++){

   $string = $string==""?'('.$array[$i].')':$string.',('.$array[$i].')';
   if($i % 100 == 0){
     $query = INSERT INTO TABLE_NAME (XXX_id) VALUES ($string);
     // execute the query
    $string = "";
  }
}

if($string != ""){
$query = INSERT INTO TABLE_NAME (XXX_id) VALUES ($string);
     // execute the query
}

1 Comment

I don't think he wants to put them all into one record, I think each one is a separate row.
0

do the following stuff in your procedure to get rid of your problems:

-- Temporary table to hold splited row value of string


drop temporary table if exists temp_convert;
        create temporary table temp_convert(split_data varchar(2056) );

       -- dynamic query to break comma separated string as rows and 

insert into column
        set @sql = concat("
        insert into temp_convert (split_data) 
                               values ('", replace(
        ( select group_concat(in_string) as converted_data), ",", "'),('"),"'
                                                   );"
        );
        prepare stmt1 from @sql;
        execute stmt1;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.