Update and insert query in one

Thread Solved

Join Date: Oct 2007
Posts: 260
Reputation: Venom Rush is an unknown quantity at this point 
Solved Threads: 2
Venom Rush's Avatar
Venom Rush Venom Rush is offline Offline
Posting Whiz in Training

Update and insert query in one

 
0
  #1
Feb 6th, 2008
Hi there

I'd like to know if it's possible to combine an update and insert query into one query. What I'd like to do is insert new data into one table and update a colum in another table at the same time.

For example, there are 3 different baskets (puppy basket, kitten basket and chick basket). I add a brown puppy to the puppy basket so I'd update the the basket table column that keeps track of the number of puppies in the puppy basket and insert into the puppy table the new puppy and it's colour.

Would I join the 2 queries with AND?
Last edited by Venom Rush; Feb 6th, 2008 at 4:26 am.
This user has a spatula. We don't know why, but we are afraid.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,761
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: Update and insert query in one

 
1
  #2
Feb 6th, 2008
As far as I know, you can't have insert and update in the same query. The only case where you can have insert and update in the same query is when you use on duplicate clause . If you are using php to insert values to the table, you can do it without any problem.
  1. $insert_query="insert into puppybasket (number_of_puppies,color) values ('val1','val2')"; //val1 has the number and val2 has the color
  2. mysql_query($insert_query);
  3. $update_query="update basket set total=total+'val1' where condition"; // total will be updated to total+number of new puppies added.
  4. mysql_query($update_query);

Cheers,
Naveen
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 260
Reputation: Venom Rush is an unknown quantity at this point 
Solved Threads: 2
Venom Rush's Avatar
Venom Rush Venom Rush is offline Offline
Posting Whiz in Training

Re: Update and insert query in one

 
0
  #3
Feb 6th, 2008
Once again nav33n saves the day...thanks
This user has a spatula. We don't know why, but we are afraid.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,761
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: Update and insert query in one

 
0
  #4
Feb 6th, 2008
heh! yw !
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 883
Reputation: pritaeas will become famous soon enough pritaeas will become famous soon enough 
Solved Threads: 142
Sponsor
pritaeas's Avatar
pritaeas pritaeas is offline Offline
Practically a Posting Shark

Re: Update and insert query in one

 
0
  #5
Feb 12th, 2008
  1. INSERT INTO table (FIELD) VALUES (value) ON DUPLICATE KEY UPDATE FIELD=value

You need to specify keys to get this to work. See the mysql docs for more info. It is quite powerful and fast.
Last edited by peter_budo; Feb 13th, 2008 at 2:32 pm. Reason: Please use [code] tags to separate your DB query from rest of the post
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 4184 | Replies: 4
Thread Tools Search this Thread



Tag cloud for MySQL
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC