DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/)
-   PHP (http://www.daniweb.com/forums/forum17.html)
-   -   MySql insert (http://www.daniweb.com/forums/thread123325.html)

jino May 9th, 2008 2:55 am
MySql insert
 
Hi all,

I want to insert multiple records into a table using one query.
And my query is

INSERT INTO table2(id,message)
SELECT id FROM table1 WHERE condn,'hello'

but it displays an error....

Can you help me?
Thanx in advance....

jino

nickyspace May 9th, 2008 3:39 am
Re: MySql insert
 
hi
Jino

what exactly you want to insert
insert data through forms or from one table to another table.

regards
nickyspace

Quote:

Originally Posted by jino (Post 603303)
Hi all,

I want to insert multiple records into a table using one query.
And my query is

INSERT INTO table2(id,message)
SELECT id FROM table1 WHERE condn,'hello'

but it displays an error....

Can you help me?
Thanx in advance....

jino


mike.prinsloo May 9th, 2008 4:07 am
Re: MySql insert
 
It might help to post the error here as well.

Notice that you are trying to INSERT two values, id and message, but you are only SELECTING one value, id. Your query should look something like the following:

INSERT INTO table2(id, message) SELECT id, message FROM table1 WHERE some_condition

jino May 9th, 2008 5:04 am
Re: MySql insert
 
dear mike,

actually id is from a table ie:from table1 and the message is a variable that is initialize dynamically.

mike.prinsloo May 9th, 2008 5:52 am
Re: MySql insert
 
Try this:

INSERT INTO table2(id, message) VALUES((SELECT id FROM table1 where some_condition), 'Hello')

jino May 9th, 2008 6:19 am
Re: MySql insert
 
Thank you mike.....i got it....

jino May 9th, 2008 7:14 am
Re: MySql insert
 
sorry mike.....

there is another problem ...if my subquery returns more than one records it displays an error

mike.prinsloo May 9th, 2008 8:05 am
Re: MySql insert
 
You'll have to write this in two queries:

First do the SELECT id FROM table1 WHERE some_condition

Then do the INSERT INTO table2(id, message) VALUES (first_item_from_select, 'Hello 1'), (second_item_from_select, 'Hello 2'), and so on...


All times are GMT -4. The time now is 8:42 pm.

Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC