Search
Left Quote    It is possible to store the mind with a million facts and still be entirely uneducated.
- Alec Bourne    
Right Quote
 
[login] | [Register]
Forum Index » PHP and mySQL » Viewing Topic and Replies
Post Reply


        Viewing Topic: add/update in mysql
  This user is offline  misterhaan
  Subject: "add/update in mysql" Posted: @ 2:56 pm on Jun 08 2005   

Member #: 5
Rank: User - (213)
Since: 02/11/05
Posts: 148
From: chair

i've run into my second situation now where it makes sense for me to run a single query that either adds a new row to a table or updates an existing row. i'm not sure if this is possible though as i haven't done it before.

in both cases the table has a primary key. i would specify the value of the primary key i want to add or insert. if it's already in the table i would update that row, otherwise add a new row. in one case i would be replacing all the other data and in the other case i would leave it as is (or use the default if adding a new row).

i know i can do this by running a select query first to see if the row already exists and then doing either an insert or an update, but i thought i'd try to find a better way first


please note that the above post is likely made up in its entirety.
    Viewed: 5,154 Times | Reply to This | To top
  This user is offline  bs0d
  Subject: "re: add/update in mysql" Posted: @ 3:06 pm on Jun 08 2005    

Member #: 1
Rank: Admin. - (1,505)
Since: 02/06/05
Posts: 600
From: USA

yea, because if you do a query to see if its exists, then isnt it almost defeating the purpose (multiple queries)?

I know where you're going with this, it could be useful IMO




-bs0d | AllSyntax.com

    Viewed: 5,146 Times | Reply to This | To top
  This user is offline  misterhaan
  Subject: "re: add/update in mysql" Posted: @ 3:50 pm on Jun 08 2005    

Member #: 5
Rank: User - (213)
Since: 02/11/05
Posts: 148
From: chair

i found something that solves one of my problems: replace queries. this automatically deletes the previous row if there's already one with the primary key, so it will work where i want to replace all of the data.

i think the other case will actually work using an insert -- if the primary key i'm trying to use is already in the table it will probably just fail and not change anything, which is actually the desired result.




please note that the above post is likely made up in its entirety.

    Viewed: 5,142 Times | Reply to This | To top
Viewing Page: 1 of 1


1 |

This Topic is Locked






"AllSyntax.com" Copyright © 2002-2018; All rights lefted, all lefts righted.
Privacy Policy  |  Internet Rank