| | |
Update and insert query in one
Please support our MySQL advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
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?
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.
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.
Cheers,
Naveen
php Syntax (Toggle Plain Text)
$insert_query="insert into puppybasket (number_of_puppies,color) values ('val1','val2')"; //val1 has the number and val2 has the color mysql_query($insert_query); $update_query="update basket set total=total+'val1' where condition"; // total will be updated to total+number of new puppies added. mysql_query($update_query);
Cheers,
Naveen
Ignorance is definitely not bliss!
*PM asking for help will be ignored*
*PM asking for help will be ignored*
MySQL Syntax (Toggle Plain Text)
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
![]() |
Similar Threads
- JSTL compare string (JSP)
- Please help with JSP mysql update query (JSP)
- SQL Delete/Update Error (ColdFusion)
- help!! update mysql problem (MySQL)
- HTTP_REFERER and something else (PHP)
- download and upload (ASP.NET)
- Unable to insert form data into a database (ASP)
- need to update each records after record deleted (PHP)
- This ought to be simple - extra spaces (PHP)
Other Threads in the MySQL Forum
- Previous Thread: Populating a MySQL table with data from a csv file
- Next Thread: Where to get free MySQL?
Views: 4184 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for MySQL
"use" 1 agplv3 alfresco amazon api artisticlicense aws breathalyzer camparingtocolumns changingprices cmg communityjournalism contentmanagement count court crm data database design developer development drupal ec2 eliminate email enter enterprise error eudora facebook form foss gartner gnu gpl greenit groklaw groupware hiring hyperic images innerjoins insert ip joebrockmeier join journalism keywords kickfire laptop law legal linux maintenance managing matchingcolumns metron micromanage microsoft microsoftexchange mindtouch montywidenius mozilla music mysql mysqlcolumnupdating mysqldatetimeordermax() mysqlindex mysqlinternalqueries mysqlquery mysqlsearch news open-xchange opendatabasealliance opengovernment opensource operand oracle pdf penelope php priceupdating query referencedesign remove reorderingcolumns resultset saas sharepoint sourcecode spotify sql statement sugarcrm syntax techsupport transparency update virtualization






