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');
Related
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);
I know this is possible using transaction but I want to do it using single mysql query.
this is which common format transaction
START TRANSACTION;
SELECT #A:=SUM(salary) FROM table1 WHERE type=1;
UPDATE table2 SET summary=#A WHERE type=1;
COMMIT;
but I need to know a single mysql query is possible?
Normally, you can not do this in MySQL. But you can do this using concat query.
INSERT INTO your_table
(value1,value2,value3)
VALUES
(a,b,c),
(d,e,f),
(i,j,k);
But this is not your question answer. so Your question answer is NO. MYSQL is not support it still now.
No, it can't be done in single statement like
insert into table1,table2
either you do separately like
insert into table1 ...
insert into table2 ...
(OR)
Wrap the insert statements in stored procedure and call that procedure like
create procedure sp_insert_multiple
as
begin
insert into table1 ...
insert into table2 ...
end
Call the SP
exec sp_insert_multiple
You can't do this. However, you can use a transaction and have both of them be contained within one transaction.
START TRANSACTION;
INSERT INTO table_1 VALUES ('1','2','3');
INSERT INTO table_2 VALUES ('one','two','three');
COMMIT;
See the following rule
Normally it is not possible to insert multiple table in single query. you can insert multiple row in a single table . like as
INSERT INTO tbl_test
(a1,a2,a3)
VALUES
(1,2,3),
(4,5,6),
(7,8,9);
you can do this in Oracle
by using procedure you can insert
create procedure insert_query
as
begin
insert into tbl_test1(a1,a2,a3) VALUES (1,2,3)
insert into tbl_test2 (b1,b2,b3) VALUES (1,2,3)
end
you can do that like coz
MySQL doesn't support multi-table insertion in a single INSERT statement.
INSERT INTO NAMES VALUES(...)
INSERT INTO PHONES VALUES(...)
here is a link of detail answer..
sql - insert into multiple tables in one query
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 am trying to make a query that will insert multiple values from my form into a single row in my table.
$q2="INSERT INTO tbl_Answer('Answer')VALUES ('$A1'),('$A2'),('$A3'),('$A4'),('$A5')";
Everything I have found tells me this should work using PHP with a MySQl database. Any ideas if I have done something wrong with the syntax or where my issue is? Thanks
INSERT INTO tbl_Answer (Answer) VALUES ('$A1'),('$A2'),('$A3'),('$A4'),('$A5')
Is the same as saying:
INSERT INTO tbl_Answer (Answer) VALUES ('$A1')
INSERT INTO tbl_Answer (Answer) VALUES ('$A2')
INSERT INTO tbl_Answer (Answer) VALUES ('$A3')
INSERT INTO tbl_Answer (Answer) VALUES ('$A4')
INSERT INTO tbl_Answer (Answer) VALUES ('$A5')
What exactly are you looking for?
Here is a demo of your original query working: http://sqlfiddle.com/#!2/e20fc/1
Looks like the value of $A1-$A5 is throwing the query off. Are you receiving any error?
We might need more information however a couple of things:
If you are using mysql in unix based system tables are case sensitive.
Second, should you use ` instead of ' on the name of your columns?(and i would say they are both innecesary) like:
INSERT INTO tbl_Answer(`Answer`) or even better INSERT INTO tbl_Answer(Answer)
You need more fields in your table. You should have a field for each of your variables.
You have a minor problem with your query syntax.
$q2 = "INSERT INTO tbl_Answer
('A1', 'A2', 'A3', 'A4', 'A5')
VALUES
('$A1', '$A2', '$A3', '$A4', '$A5');";
what am doing is clonning an old database to new one with different structure but should have same old data
is it possible to do an insert into insert that return id of the insert query
INSERT INTO tab1 (nom,id_2) VALUES
("jack",(INSERT INTO tab2 (pass) VALUES ("1234")));
in INSERT INTO tab2 (pass) VALUES ("1234") i want to get the id of the insert
but no luck!! (i want it in one sql query (i already know about mysql_insert_id) )
thanks in advance
You cant do this on a single query - use mysql_insert_id() to get the last inserted id for example :
mysql_query('INSERT INTO tab2 (pass) VALUES ("1234")');
$lastid = mysql_insert_id();
mysql_query('INSERT INTO tab1 (nom,id_2) VALUES ("jack",'.$lastid.')');
insert id can be retrieved in mysql as well, so here is a version with no PHP involved (it however performs 2 queries)
INSERT INTO tab2 (pass) VALUES ("1234");
INSERT INTO tab1 (nom,id_2) VALUES("jack",LAST_INSERT_ID());
Lock the table, insert, select max(id), unlock table. This is the only way outside creating your own sequence tables like in postgres if you are adverse for some reason to the last insert id functions. This approach however is going to be like pouring tar into a blender if your table is moderate to high writes.
You can't do this as a single insert because inserts are atomic that is, the ID isn't determined until the statement completes.
Wrap both statements in a transaction and you will get your ID, and atomicity.