User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 426,482 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,165 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 380 | Replies: 7
Reply
Join Date: Feb 2008
Location: Cochin
Posts: 67
Reputation: jino is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 2
jino's Avatar
jino jino is offline Offline
Junior Poster in Training

MySql insert

  #1  
May 9th, 2008
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
We only have to do a very few things right in our life, so long as we don't do too many things wrong.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2008
Posts: 12
Reputation: nickyspace is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
nickyspace's Avatar
nickyspace nickyspace is offline Offline
Newbie Poster

Re: MySql insert

  #2  
May 9th, 2008
hi
Jino

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

regards
nickyspace

Originally Posted by jino View Post
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
Reply With Quote  
Join Date: May 2008
Posts: 3
Reputation: mike.prinsloo is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
mike.prinsloo mike.prinsloo is offline Offline
Newbie Poster

Re: MySql insert

  #3  
May 9th, 2008
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
Reply With Quote  
Join Date: Feb 2008
Location: Cochin
Posts: 67
Reputation: jino is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 2
jino's Avatar
jino jino is offline Offline
Junior Poster in Training

Re: MySql insert

  #4  
May 9th, 2008
dear mike,

actually id is from a table ie:from table1 and the message is a variable that is initialize dynamically.
We only have to do a very few things right in our life, so long as we don't do too many things wrong.
Reply With Quote  
Join Date: May 2008
Posts: 3
Reputation: mike.prinsloo is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
mike.prinsloo mike.prinsloo is offline Offline
Newbie Poster

Re: MySql insert

  #5  
May 9th, 2008
Try this:

INSERT INTO table2(id, message) VALUES((SELECT id FROM table1 where some_condition), 'Hello')
Reply With Quote  
Join Date: Feb 2008
Location: Cochin
Posts: 67
Reputation: jino is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 2
jino's Avatar
jino jino is offline Offline
Junior Poster in Training

Re: MySql insert

  #6  
May 9th, 2008
Thank you mike.....i got it....
We only have to do a very few things right in our life, so long as we don't do too many things wrong.
Reply With Quote  
Join Date: Feb 2008
Location: Cochin
Posts: 67
Reputation: jino is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 2
jino's Avatar
jino jino is offline Offline
Junior Poster in Training

Re: MySql insert

  #7  
May 9th, 2008
sorry mike.....

there is another problem ...if my subquery returns more than one records it displays an error
We only have to do a very few things right in our life, so long as we don't do too many things wrong.
Reply With Quote  
Join Date: May 2008
Posts: 3
Reputation: mike.prinsloo is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
mike.prinsloo mike.prinsloo is offline Offline
Newbie Poster

Re: MySql insert

  #8  
May 9th, 2008
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...
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb PHP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 4:27 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC