User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the MySQL section within the Web Development category of DaniWeb, a massive community of 425,954 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 1,629 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 MySQL advertiser: Programming Forums
Views: 1483 | Replies: 4 | Solved
Reply
Join Date: Oct 2007
Posts: 180
Reputation: Venom Rush is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 2
Venom Rush's Avatar
Venom Rush Venom Rush is offline Offline
Junior Poster

Update and insert query in one

  #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 3:26 am.
This user has a spatula. We don't know why, but we are afraid.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 240
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Sensei

Re: Update and insert query in one

  #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
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

*PM asking for help will be ignored*
Reply With Quote  
Join Date: Oct 2007
Posts: 180
Reputation: Venom Rush is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 2
Venom Rush's Avatar
Venom Rush Venom Rush is offline Offline
Junior Poster

Re: Update and insert query in one

  #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  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 240
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Sensei

Re: Update and insert query in one

  #4  
Feb 6th, 2008
heh! yw !
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

*PM asking for help will be ignored*
Reply With Quote  
Join Date: Jul 2006
Location: Remunj
Posts: 207
Reputation: pritaeas is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 25
pritaeas's Avatar
pritaeas pritaeas is offline Offline
Posting Whiz in Training

Re: Update and insert query in one

  #5  
Feb 12th, 2008
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 1:32 pm. Reason: Please use [code] tags to separate your DB query from rest of the post
Reply With Quote  
Reply

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

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

 

Thread Tools Display Modes

Similar Threads
Other Threads in the MySQL Forum

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