thara Posted September 6, 2013 Share Posted September 6, 2013 Suppose we have a mysql table something like this id, userid, days, start, and close and we have data for each columns like this - 1, 3, mon, 01.00, 02.00, 2, 3, tue, 03.00, 05.00, 3, 3, wed, 04.00, 06.00, 4, 3, thu, 05.00, 07.00, 5, 3, fri, 06.00, 08.00, 6, 3, sat, 07.00, 10.00, 7, 3, sun, 08.00, 12.00, with this data, I need to update or insert my table. (if userid is not exist in table it should be insert or if userid exist in db it should be update.) Can I know, is there a way to make a mysql single query for this? I tried it with INSERT ... ON DUPLICATE KEY UPDATE there I can only edit single row that mean I can not insert or update table with multiple rows using INSERT ... ON DUPLICATE KEY UPDATE. At the moment I have used 2 different queries for inset and update This is my insert query - $q = "INSERT INTO useravailability (userid, days, opentime, closetime) VALUES (?, 'Monday', ?, ?), (?, 'Tuesday', ?, ?), (?, 'Wednesday', ?, ?), (?, 'Thursday', ?, ?), (?, 'Friday', ?, ?), (?, 'Saturday', ?, ?), (?, 'Sunday', ?, ?)"; $stmt = mysqli_prepare($dbc, $q); mysqli_stmt_bind_param($stmt, 'issississississississ', $userId, $monOpen, $monClose, $userId, $tueOpen, $tueClose, $userId, $wedOpen, $wedClose, $userId, $thuOpen, $thuClose, $userId, $friOpen, $friClose, $userId, $satOpen, $satClose, $userId, $sunOpen, $sunClose); // Execute the query: mysqli_stmt_execute($stmt); Quote Link to comment https://forums.phpfreaks.com/topic/281909-how-i-make-a-single-query-for-both-insert-and-update/ Share on other sites More sharing options...
StefanRSA Posted September 6, 2013 Share Posted September 6, 2013 Why not doing a simple Lookup first to see if the userid exists and then do the Update or insert? Quote Link to comment https://forums.phpfreaks.com/topic/281909-how-i-make-a-single-query-for-both-insert-and-update/#findComment-1448433 Share on other sites More sharing options...
thara Posted September 6, 2013 Author Share Posted September 6, 2013 Why not doing a simple Lookup first to see if the userid exists and then do the Update or insert? Yes Its ok. I have already done it. But reason to post here, I am looking for a solution with single query for both insert and update using INSERT ... ON DUPLICATE KEY UPDATE. Quote Link to comment https://forums.phpfreaks.com/topic/281909-how-i-make-a-single-query-for-both-insert-and-update/#findComment-1448436 Share on other sites More sharing options...
Barand Posted September 6, 2013 Share Posted September 6, 2013 To use ON DUPLICATE KEY you first need a unique key. Userid is not unique so the unique key would need to be (userid, days). Then when you try to add, say, (3, 'Monday') again there would be a duplicate so you can update the time fields Quote Link to comment https://forums.phpfreaks.com/topic/281909-how-i-make-a-single-query-for-both-insert-and-update/#findComment-1448438 Share on other sites More sharing options...
thara Posted September 6, 2013 Author Share Posted September 6, 2013 @Barand, Thanks for response. I tried it, this is my table structure - CREATE TABLE availability ( availabilityid SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT, userid SMALLINT UNSIGNED NOT NULL, days VARCHAR(10) NOT NULL, opentime VARCHAR( DEFAULT NULL, closetime VARCHAR( DEFAULT NULL, PRIMARY KEY (id), UNIQUE (userid, days) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; Quote Link to comment https://forums.phpfreaks.com/topic/281909-how-i-make-a-single-query-for-both-insert-and-update/#findComment-1448441 Share on other sites More sharing options...
Barand Posted September 6, 2013 Share Posted September 6, 2013 I tried it,..... And...? Quote Link to comment https://forums.phpfreaks.com/topic/281909-how-i-make-a-single-query-for-both-insert-and-update/#findComment-1448444 Share on other sites More sharing options...
Solution thara Posted September 6, 2013 Author Solution Share Posted September 6, 2013 yes I tried it like this and it works me. INSERT INTO availability (userid, days, opentime, closetime) VALUES (?, 'Monday', ?, ?), (?, 'Tuesday', ?, ?), (?, 'Wednesday', ?, ?), (?, 'Thursday', ?, ?), (?, 'Friday', ?, ?), (?, 'Saturday', ?, ?), (?, 'Sunday', ?, ?) ON DUPLICATE KEY UPDATE opentime = VALUES(opentime), closetime = VALUES(closetime) ; is my way correct? or is there any to do this? Quote Link to comment https://forums.phpfreaks.com/topic/281909-how-i-make-a-single-query-for-both-insert-and-update/#findComment-1448453 Share on other sites More sharing options...
Barand Posted September 6, 2013 Share Posted September 6, 2013 That looks good - I see you found the VALUES() function needed for multiple updates Quote Link to comment https://forums.phpfreaks.com/topic/281909-how-i-make-a-single-query-for-both-insert-and-update/#findComment-1448454 Share on other sites More sharing options...
thara Posted September 6, 2013 Author Share Posted September 6, 2013 @Barand, Is there a way to use 'SELECT' with this? Quote Link to comment https://forums.phpfreaks.com/topic/281909-how-i-make-a-single-query-for-both-insert-and-update/#findComment-1448461 Share on other sites More sharing options...
Barand Posted September 6, 2013 Share Posted September 6, 2013 In what way? You can insert data into one table that is selected from another. Quote Link to comment https://forums.phpfreaks.com/topic/281909-how-i-make-a-single-query-for-both-insert-and-update/#findComment-1448463 Share on other sites More sharing options...
thara Posted September 6, 2013 Author Share Posted September 6, 2013 No I mean, is there any way to get data after INSERTING or UPDATING with a single query... Quote Link to comment https://forums.phpfreaks.com/topic/281909-how-i-make-a-single-query-for-both-insert-and-update/#findComment-1448474 Share on other sites More sharing options...
kicken Posted September 6, 2013 Share Posted September 6, 2013 No, there is not a way to INSERT/UPDATE data and SELECT the data back out from the same query. You'd have to do a separate select query after your insert/update query. Quote Link to comment https://forums.phpfreaks.com/topic/281909-how-i-make-a-single-query-for-both-insert-and-update/#findComment-1448502 Share on other sites More sharing options...
thara Posted September 7, 2013 Author Share Posted September 7, 2013 No I mean, is there any way to get data after INSERTING or UPDATING with a single query... Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/281909-how-i-make-a-single-query-for-both-insert-and-update/#findComment-1448562 Share on other sites More sharing options...
priyankagound Posted September 12, 2013 Share Posted September 12, 2013 Here is a solution which will surely help you... This is the example which i tried.. If you add a unique index on (userid, days): -- run this only onceALTER TABLE availability ADD UNIQUE INDEX userid_days_UQ -- just a name for the index(userid, days) ; then you can use the ON DUPLICATE KEY UPDATE syntax: $q = " INSERT INTO availability (userid, days, opentime, closetime) VALUES (?, 'Monday', ?, ?),(?, 'Tuesday', ?, ?),(?, 'Wednesday', ?, ?),(?, 'Thursday', ?, ?),(?, 'Friday', ?, ?), (?, 'Saturday', ?, ?), (?, 'Sunday', ?, ?)ON DUPLICATE KEY UPDATEopentime = VALUES(opentime),closetime = VALUES(closetime) ; "; Quote Link to comment https://forums.phpfreaks.com/topic/281909-how-i-make-a-single-query-for-both-insert-and-update/#findComment-1449209 Share on other sites More sharing options...
mac_gyver Posted September 12, 2013 Share Posted September 12, 2013 @priyankagound if you look at post #7 in this thread, you will see that the OP already did that. marking this thread as solved... Quote Link to comment https://forums.phpfreaks.com/topic/281909-how-i-make-a-single-query-for-both-insert-and-update/#findComment-1449231 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.