I record the ip address of visitor and store it into mysql database. If the visitor gets a different ip, it will also update the record. I use the following code to do the update and values in following example is just for testing.
insert into visiter_info values ('1344594088179','0','100.100.100.100','china','300x600','IOS','firefox','')
ON DUPLICATE KEY UPDATE
ip_address=concat(ip_address,'|','100.100.100.100'),
location=concat(location,'|','china'),
screen_res=concat(screen_res,'|','300x600'),
os=concat(os,'|','IOS'),
brower=concat(brower,'|','firefox')
It works, but now problem comes, how can I check if there is a record in the database? like this: visitor comes again, with ip 100.100.100.100. Mysql don't know there is a record, and it will re-record. How to check that if contains a sub string before insert?
if not exists(select * from visiter_info where ip_address='100.100.100.100')
insert into visiter_info values ('1344594088179','0','100.100.100.100','china','300x600','IOS','firefox','')
ON DUPLICATE KEY UPDATE
ip_address=concat(ip_address,'|','100.100.100.100'),
location=concat(location,'|','china'),
screen_res=concat(screen_res,'|','300x600'),
os=concat(os,'|','IOS'),
brower=concat(brower,'|','firefox')
Related
I have an app that has to import TONS of data from a remote source. From 500 to 1500 entries per call.
Sometimes some of the data coming in will need to replace data already stored in the dB. If I had to guess, I would say once in 300 or 400 entries would one need to be replaced.
Each incoming entry has a unique ID. So I am trying to figure out if it is more efficient to always issue a delete command based on this ID or to check if there is already an entry THEN delete.
I found this SO post where it talks about the heavy work a dB has to do to delete something. But it is discussing a different issue so I'm not sure if it applies here.
Each incoming entry has a unique ID. So I am trying to figure out if it is more efficient to always issue a delete command based on this ID or to check if there is already an entry THEN delete.
Neither. Use INSERT ... ON DUPLICATE KEY UPDATE ....
Since you are using MySQL and you have a unique key then let MySQL do the work.
You can use
INSERT INTO..... ON DUPLICATE KEY UPDATE......
MySQL will try to insert a new record in the table, is the unique value exists in the table then MySQL will update all the field that you have set after the update
You can read more about the INSERT INTO..... ON DUPLICATE KEY UPDATE...... syntax on
http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html
I'm trying to write a sql statement that does an insert if a certain row id doesn't exist else updates.
So I've read and the below should work however it just freezes my SQLite database browser:
Any ideas?
INSERT INTO details
(id, name, age)
VALUES
(“1”, "mike", "22")
ON DUPLICATE KEY UPDATE
name = “mike”
age = “22”
it's hard to find current posts on the subject, but it appears that "ON DUPLICATE KEY UPDATE" does not exist in sqlite. you'll need to find an alternate way to do it. the two methods i've found are
1) ON CONFLICT...REPLACE http://www.sqlite.org/lang_conflict.html which doesn't update but deletes the given row then inserts it with the new values and
2) try/catch clause, something like this: http://blog.client9.com/2007/11/21/sqlite3-and-on-duplicate-key-update.html which is programmatically catching the error from the conflict, and enacting the update when the error occurs
I store path of profile picture of my users in database and images are saved with the id of user as a suffix.Now as the same user again uploads its image,I want the path to be updated.I have three columns in the image table.
1.username
2.path
3.date
I tried to use REPLACE instead of INSERT like this
$query="REPLACE INTO img_upload(username,path,created) values('".$session_user['college_id']."','".$target."','".date('Y-m-d H:i:s',time())."')";
but it didn't worked.
username is primary key.
I can't really tell from your question if you want INSERT INTO ON DUPLICATE KEY UPDATE or or an UPDATE statement. This will insert a row if the key is not a duplicate and UPDATE it otherwise.
INSERT INTO img_upload (username,path,created)
VALUES ('".$session_user['college_id']."','".$target."','".date('Y-m-d H:i:s',time())."')"
ON DUPLICATE KEY UPDATE path=$target, created=date('Y-m-d H:i:s',time())."')
Or a simple update.
UPDATE img_upload SET path=$target, date=date('Y-m-d H:i:s',time())."') WHERE username=$session_user['college_id']
i have login page with user name and password, if the user forgot his/her password then there is a link of forgot password,were one has puts its email id and then the default password will send to its id and it will also update it in table with update query,the problem is that when in future we give this project to client we have truncate whole data and give him empty database, at that time update query won't work at that moment i need to use insert query, so i need a trigger query which fires on insert as well as on update query also later on when data is filled, please help because i never used trigger and i don't know its syntax, please explain me by showing an example
You do not need a trigger. Just run an INSERT INTO...ON DUPLICATE KEY UPDATE query. It will insert a row, but if a row with the same primary key already exists, update the column in the existing row instead.
http://dev.mysql.com/doc/refman/5.1/en/insert-on-duplicate.html
I'm trying to create a code for a single button where it will perform either of two actions where it will add to the database if the user currently don't have the record while it will update the user's record if the user has records already. I've done it like this:
if() {
mysql_query("INSERT INTO table...");
}
else {
mysql_query("UPDATE table SET...");
}
Is it possible?
Yes, what you've written will work. If you have a way to know if there already exists a row or not without making an additional query just for this bit of code, then do exactly as you wrote.
If, however, you planned to first SELECT from the table to see if a row exists, then conditionally INSERT or UPDATE, you will perform more queries than necessary.
It would be better to either:
Have a PRIMARY KEY or other constraint on the table prevent duplicate INSERTs. Then issue an INSERT ... ON DUPLICATE KEY UPDATE query. This will attempt to INSERT the row, and if it is a duplicate, automatically perform the specified UPDATE to that row instead.
Issue the UPDATE query and check mysql_affected_rows to see if it updated an existing row. If not, then issue the INSERT query to create the new row.
Which one is more appropriate depends on your application.
you can use INSERT ... ON DUPLICATE KEY UPDATE Syntax like:
INSERT INTO table (a,b,c) VALUES (1,2,3)
ON DUPLICATE KEY UPDATE c=c+1;
http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html
If you have properly set unique keys, you should use REPLACE so you could remove the if.
REPLACE INTO table VALUE (...);
Pay attention that this is a MySQL extension, thus not portable to other DBs.
Yes, you could try the insert then if it fails try the update.
But you could use the MYSQL sql "REPLACE" keyword, which will insert a new record if it doesn't exist or delete the existing record and insert your new one if it does.
You could also use the INSERT ... ON DUPLICATE KEY UPDATE syntax
(explained here - Link to MYSQL ref which seems to be the closest fit to your requirement.
yes it is possible
first write a query for check that record is already exist or not.
Yes it is possible , it will work