You might want to update or insert Database single statement rather than checking the record is inserted or and again go to the update.
Create the sample table as below
CREATE TABLE IF NOT EXISTS `tbl_test` (
cat_id int AUTO_INCREMENT,
name varchar(50),
primary key(cat_id)
)
add some records to tbl_test
INSERT INTO tbl_test (name) VALUES ('cat 111'),('cat 2'),('cat 3');
and finally you can update the record or insert by following
INSERT INTO tbl_test (cat_id,name) VALUES (1,' cat 1 ')
ON DUPLICATE KEY UPDATE name=' cat 1 '
That's IT.
Cheers
But using following SQL statement you can run First run and check Insert statement and if that fails Update statement after that.
Assume my table structure as follows
Create the sample table as below
CREATE TABLE IF NOT EXISTS `tbl_test` (
cat_id int AUTO_INCREMENT,
name varchar(50),
primary key(cat_id)
)
add some records to tbl_test
INSERT INTO tbl_test (name) VALUES ('cat 111'),('cat 2'),('cat 3');
and finally you can update the record or insert by following
INSERT INTO tbl_test (cat_id,name) VALUES (1,' cat 1 ')
ON DUPLICATE KEY UPDATE name=' cat 1 '
That's IT.
Cheers
Comments
Post a Comment