The query syntax is as follows:
INSERT INTO sent (username,password) VALUES
('user','user2','user3','user4','user5','user6'),
('pass','pass2','pass3','pass4','pass5','pass6')
Resource: http://dev.mysql.com/doc/refman/5.5/en/insert.html
The mysql_error() always showing me this:
Column count doesn't match value count at row 1
I have no idea what should I do. Now it's time to ask you about this.
You specified 2 columns with 6 values. The number of columns and values has to match. What you want is this:
INSERT INTO sent (username,password) VALUES ('user','pass'),('user2','pass2'),('user3','pass3'),('user4','pass4'),('user5','pass5'),('user6','pass6')
See the MySQL documentation for more details:
INSERT statements that use VALUES syntax can insert multiple rows. To
do this, include multiple lists of column values, each enclosed within
parentheses and separated by commas. Example:
INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);
Related
I am inserting data from a excel sheet to database. All rows in a single query.
I searched and found that to insert multiple rows in a single query i have to make query like this
INSERT INTO table_name(c1,c2) VALUES( (value1,value2),(value1,value2))
This is how my INSERT INTO query looks like for single row
INSERT INTO December_2015(S_No,Zone,State,City2,VM_Town,Distibutor_Code,Distributor_Name,Dealer_Code,Dealer_Name,Category,Address,Location,Contact,Mobile_No,Visit_1,Visit_2,Visit_3,Visit_4,Visit_5,Visit_6) VALUES(('1','South','Telanagana','Hyderabad','Y','1006704','Sai Santhoshi Enterprises','TG000999','Sree Laxmi Mobiles','A','F4,anthem Arcade,gujarathi Galli,koti ','Koti','Rajesh','8790575680','7-Nov','18-Nov','28-Nov','','',''))
When i have multiple rows to insert my INSERT INTO query looks like this for multiple rows
INSERT INTO December_2015(S_No,Zone,State,City2,VM_Town,Distibutor_Code,Distributor_Name,Dealer_Code,Dealer_Name,Category,Address,Location,Contact,Mobile_No,Visit_1,Visit_2,Visit_3,Visit_4,Visit_5,Visit_6) VALUES(('1','South','Telanagana','Hyderabad','Y','1006704','Sai Santhoshi Enterprises','TG000999','Sree Laxmi Mobiles','A','F4,anthem Arcade,gujarathi Galli,koti ','Koti','Rajesh','8790575680','7-Nov','18-Nov','28-Nov','','',''),('2','South','Telanagana','Hyderabad','Y','1006704','Sai Santhoshi Enterprises','TG000997','National Marketing','C','Shop No:28,2nd Floor,anthem Arcade,Gujarati Galli,koti ','Koti','Ramesh','9989014926','7-Nov','18-Nov','28-Nov','','',''))
and thats why i have two brackets in my query but it returns error Operand should contain 1 column(s) What is the problem exactly?
You don't need an outside bracket. Just brackets around each record.
INSERT INTO table_name(col1, col2, col3) VALUES (1,2,3), (4,5,6);
First question is I have a sql query like below.. And how to get ids of test, test1 and test2?
INSERT IGNORE INTO tags (tag_name) VALUES ('test'),('test1'),('test2')
There is a table to match tag_id and ann_id(announcements).The structure of the table is
tag_id(int 6)
ann_id(int 6)
I want to avoid duplicate lines. Is there anything that i can do with mysql for solve this problem? Or I must create a select query and count?
I'm attempting to do a "bulk" insert with the following query:
INSERT INTO `order_status_histories` VALUES ('3602','52efabe9-5f8c-4512-a994-3227c63dd20e','1','','Order recieved','2014-02-03 16:47:05','2014-02-03 16:47:05'),('3603','52eff713-54fc-4be0-9389-68d5c63dd20e','1','','Order recieved','2014-02-03 22:07:47','2014-02-03 22:07:47'),('3604','52effd1a-bc14-4095-97fd-6d46c63dd20e','1','','Order recieved','2014-02-03 22:33:30','2014-02-03 22:33:30')
However, it is failing, because the first column in the table is recordID, and does not exist in the insert statement.
How can I get around this issue?
When you use
INSERT INTO `order_status_histories` VALUES (....)
you need to pass each value that the table structure requires, in the correct order. You can omit fields from the values if they have a default value in the table structure.
Most of the time, it's better to explicitly list the fields that you want to insert to - if nothing else, it means that if the table structure changes, you won't need to re-write your SQL statements. Try changing your SQL statement so it's structured as:
INSERT INTO `order_status_histories` (field1, field2, ....) VALUES ('value1', 'value2', ....)
I have a 'location' table and a 'location_detail'[for inserting different language data] table. 'location_detail' contains FK of location table
I need to enter multiple location at a time. So what I am doing is:
run a 'for' loop inside that first I enter data into 'location' table get the loc_id then insert into location_detail table[Here in 'location_detail' table if more than one language present, again I want to run the query multiple times].
So if I want to add 3 locations -> Outer 'for' loop will run 3 times
total no of query exec. is 6 [If more than one language is present this will multiple]
==>My aim is to insert all 3(say) locations into 'location' table using multiple insert in a single statement and get all 3 last_insert_ids.
==>Next I can run single statement multiple insert query for adding into 'location_details' table
Here, how will I get this last_insert_ids in an array?
I'll do this in the same transaction/connection :
INSERT INTO location (col1, col2) VALUES (val1a, val2a);
SET #string_ids = LAST_INSERT_ID();
INSERT INTO location (col1, col2) VALUES (val1b, val2b);
SET #string_ids = CONCAT(#string_ids , ",", LAST_INSERT_ID());
INSERT INTO location (col1, col2) VALUES (val1c, val2c);
SET #string_ids = CONCAT(#string_ids , ",", LAST_INSERT_ID());
SELECT #string_ids ;
Then in php, I would explode this variable :
$array_ids = explode (",", $string_ids ).
Then build your request with php :
INSERT INTO location_detail(id, fk_id) VALUES
//foreach loop
(val_id1b, $array_ids[$i] )
(val_id2b, $array_ids[$i] )
(val_id3b, $array_ids[$i] )
I haven't tried the #array_ids but it should work.
Look at this link for more help if you need it.
I hope it fits your needs.
You can use transaction and get last_insert_id and calculate previous id's relate to your AUTO_INCREMENT settings.
Important
If you insert multiple rows using a single INSERT statement, LAST_INSERT_ID() returns the value generated for the first inserted row only. The reason for this is to make it possible to reproduce easily the same INSERT statement against some other server.
from Last Insert ID documentation.
I need to insert same data to my MySQL table without having PHP loop. The reason why I'm doing this is that because I have a column with Auto_Increment feature and that column associates with other table.
So, I just need to insert some exactly same data and it's multiple rows (dynamic) but by using single INSERT syntax below :
INSERT INTO outbox_multipart (TextDecoded) VALUES ('$SMSMessage')
how to have this single INSERT syntax, but produce n number of rows?
You can do this:
INSERT INTO outbox_multipart (TextDecoded) VALUES ('$SMSMessage')
, ('$SMSMessage2'), ('$SMSMessage3'), ('$SMSMessage4');
INSERT INTO outbox_multipart (TextDecoded) VALUES ('$SMSMessage'),('$SMSMessage')
,('$SMSMessage'),('$SMSMessage')
if done dynamically,
$n=5;
for ($i=0;$i<$n;$i++){$values.="('$SMSMessage'),";}
$values=substr($values,0,-1);
And the SQL be:
INSERT INTO outbox_multipart (TextDecoded) VALUES $values
mysql_query("INSERT INTO `table`(`this`) VALUES (`that`); INSERT INTO `table`(`this`) VALUES (`that`);");
try something like this
$sql = mysql_query("INSERT INTO outbox_multipart (TextDecoded) VALUES ('$SMSMessage')
, ('$SMSMessage'),('$SMSMessage'),('$SMSMessage')");
Also, just so you know, mysql_* have been deprecated. So, try to use mysqli_* or PDO for querying.
suppose you want 3 insertions simply do like this:
INSERT INTO outbox_multipart (TextDecoded) VALUES ('$SMSMessage'),('$SMSMessage'),('$SMSMessage');