asmith Posted July 27, 2009 Share Posted July 27, 2009 Hi, I use something like this to update some fields: $result = mysql_query("update table set visit = visit+1 where ... if mysql_affected_rows() == 0 mysql_query("insert into ... Is it possible to write such thing by replace? for example: mysql_query("replace into table (id, visit) values (id, visit+1)") or mysql_query("replace into table (id, visit) values (id, ifnull(visit, 0) + 1)") or mysql_query("replace into table (id, visit) values (id, ifnull((select visit from table where id='theNumber'), 0) + 1)") first 2 does not give any error but they simply put only 1. It doesn't add 1 to the existing value. the third gives me You can't specify target table 'table' for update in FROM clause I'm just curious about this. No reason why I wanna change to this way. Quote Link to comment https://forums.phpfreaks.com/topic/167660-solved-update-with-replace-possible/ Share on other sites More sharing options...
gizmola Posted July 27, 2009 Share Posted July 27, 2009 Replace is a non-standard MySQL extension that has nothing to do with UPDATE really. What it's really focused on is INSERT. It was added to allow people to bulk insert rows into a table, and giving you a tool to deal with problems caused by key violations that might otherwise cause the bulk insert to fail. In essence what the REPLACE does is: -Does primary key of INSERT == Primary key of a row? --- If YES ------- DELETE ROW -INSERT NEW ROW So, when you are doing an UPDATE, and specifying a value to be based on the existing value, you get the behavior you've noted, which is, that the original value get "lost" because that row gets Deleted. So in short, Replace really isn't meant to be a substitute for an UPDATE, and due to the nature of the checks involved, is also going to be slower than an Update. Replace was really added to help people who want to repeatedly re-insert a base set of rows into an existing table. Quote Link to comment https://forums.phpfreaks.com/topic/167660-solved-update-with-replace-possible/#findComment-884215 Share on other sites More sharing options...
asmith Posted July 28, 2009 Author Share Posted July 28, 2009 Thanks for your replay. You made it very clear for me. Thanks a lot Quote Link to comment https://forums.phpfreaks.com/topic/167660-solved-update-with-replace-possible/#findComment-884565 Share on other sites More sharing options...
gizmola Posted July 28, 2009 Share Posted July 28, 2009 Glad it helped. Quote Link to comment https://forums.phpfreaks.com/topic/167660-solved-update-with-replace-possible/#findComment-885275 Share on other sites More sharing options...
aschk Posted July 29, 2009 Share Posted July 29, 2009 What you can do instead if INSERT .. ON DUPLICATE KEY UPDATE... e.g. INSERT INTO table(visit) VALUES(0) ON DUPLICATE KEY UPDATE SET visit = visit+1 Quote Link to comment https://forums.phpfreaks.com/topic/167660-solved-update-with-replace-possible/#findComment-885909 Share on other sites More sharing options...
asmith Posted July 29, 2009 Author Share Posted July 29, 2009 wow yea I totally had forgotten that. cool, I'll use it. Its faster than (if mysql_affected_rows() == 0 then insert), Right? Quote Link to comment https://forums.phpfreaks.com/topic/167660-solved-update-with-replace-possible/#findComment-886070 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.