doing an update if exists

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

doing an update if exists

Post by GeXus »

Is there a way in mysql to check if a record exists and if so, then do an update, if not, do an insert statement.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

REPLACE...
GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

Post by GeXus »

Wicked.. thanks!
GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

Post by GeXus »

This actually does not seem to work.. I probably did not explain well, or maybe I did the replace wrong...

table: favorites
columns:
user_id
fav_1
fav_1_desc
fav_2
fav_2_desc
fav_3
fav_3_desc

It's posible that the fields could be either empty or not.
GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

Post by GeXus »

Ok, I think i figured out what ill do.. basically I can always insert one row for the user_id, without any data, then just do updates to the null columns.
Post Reply