on-duplicate-key

Update multiple columns on conflict postgres

℡╲_俬逩灬. 提交于 2021-01-29 06:40:50
问题 I have to write a query to update a record if it exists else insert it. I am doing this update/insert into a postgres DB. I have looked into the upsert examples and most of them use maximum of two fields to update. However, I want to update multiple columns. Example: query="""INSERT INTO table (col1,col2,col3,col4,col5,col6,col7,col8,col9,..col20) VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) ON CONFLICT(col2) DO UPDATE SET (col1,col2,col3,col4,col5,col6,col7,col8,col9,.

Why are there auto increment gaps in MySQL when violating a unique key constraint, but not a primary key constraint?

安稳与你 提交于 2020-01-05 08:05:42
问题 I understand that when using INSERT ... ON DUPLICATE KEY UPDATE in MySQL there are auto increment gaps when an insert fails. However -- I've noticed that the gaps only occur when a unique key constraint is violated. If the primary key constraint fails, no auto increment gap occurs. What's the reason for the difference between the two? Thanks! 回答1: If the auto increment field is the primary key, then duplication in the primary key can only happen if you explicitly set the auto increment field

How to On Duplicate Key Update

一曲冷凌霜 提交于 2019-12-25 03:27:24
问题 I have this query that is executed in my Python script but when its inserting into the database and finds a duplicate of my unique column it causes it to error and stops. I know I need to use On Duplicate Key Update but I'm note sure how to properly add this. My unique column 2. cur.execute("""INSERT INTO logs (1,2,3) VALUES (%s,%s,%s) """,(line[0], line[1], line[2])) If there is a duplicate to have it update that row/entry. 回答1: When I understand you correctly, what you are looking for is

INSERT INTO … ON DUPLICATE UPDATE all values

若如初见. 提交于 2019-12-24 11:28:04
问题 Can I safely update all values like this: INSERT INTO tbl_name SET `a`=:a, `b`=:b, ... `z`=:z ON DUPLICATE KEY UPDATE `a`=VALUES(`a`), `b`=VALUES(`b`), ... `z`=VALUES(`z`); I have tried it, and it works. But it's tested only on one innodb table. I have found these related questions 1, 2, 3 but there's no mention that updating ALL columns (unique and/or pk) is 100% safe. It doesn't create any error on updating PK with same value. It doesn't create any error on updating Unique col with same

Differentiate between 'no rows were affected' and rows succesfully UPDATEd--to same value (MySQL and PHP)

谁说胖子不能爱 提交于 2019-12-24 03:25:36
问题 I am executing SQL (MySQL) commands from PHP. There are several possible outcomes to each execution: Record updated to new value Record updated, but values happen to be the same Record finds no rows to update (ie, no rows match the WHERE clause) I am wondering how to differentiate between #'s 1 and 3: both cases return zero as the number of rows being affected, so: $result = $db->exec($statement) will have $result == 0 in both cases. How can I tell the difference? EDIT: I meant to ask how to

insert into select on duplicate mysql query

与世无争的帅哥 提交于 2019-12-23 04:28:07
问题 I am trying to update on duplicate record in MySQL, I have a table with many column but i want to update only some column from another table with same desc as current table but it is not updating records. my query is: insert into backup_prochart.symbol_list(ticker,end_date,cur_date) select ticker,t.end_date,t.cur_date from prochart.symbol_list t where ticker=t.ticker and ticker= 'MAY17' on duplicate key update end_date=t.end_date,cur_date=t.cur_date; another query i tried insert into backup

php: rename duplicate keys by giving number in foreach

丶灬走出姿态 提交于 2019-12-13 00:28:47
问题 i have a form data and the data's array like this: $datas=array("x-1","y-2","y-2","y-3","t-1"); my foreach loop: foreach($datas as $x => $data){ $data=explode("-",$data); if($data[0]==$data[0]+1){$n=1;}else{$n=0;} $keys[$x]=$data[0].$n++; $vals[$x]=$data[1]; } i couldn't write the true code, my 3rd line is wrong i think (if($data[0]=$data[0]+1){$n="1";}else{$n="";}) so, i wanna rename the duplicate keys by giving number. my output should be like this: x=1 y1=1 y2=2 y3=2 t1=1 回答1: Try $datas

insert, if exist update using trigger

ぐ巨炮叔叔 提交于 2019-12-12 03:23:14
问题 I want to implement a Viewed system for my website. Here is the structure of my tables: // table1 id | user_or_ip | post_id | date_time // inserting new row for each viewed // table2 id | post_id | total_viewed // getting the number of total viewed for each post Now I need to a trigger for insert/update table2 after insert in table1 . I think I have to use on duplicate key . 回答1: You can do this fairly easily. With the following 2 example table:- CREATE TABLE table1 ( id INT NOT NULL AUTO

php - update sql statement with multiples rows

那年仲夏 提交于 2019-12-12 01:45:44
问题 I know this problem has already been around but I cannot figure out how to solve it in my case. I try to use the UPDATE statement with sql but I have a problem when dealing with multiples rows. This is my code: $body = file_get_contents('php://input'); $jsonArray = json_decode($body, true); $sql = array(); foreach ($jsonArray as $row) { $sql[] = '("'.$row['firstname'].'", "'.$row['lastname'].'", "'.$row['sex'].'", "'.$row['dateOfBirth'].'", "'.$row['email'].'")'; } $column_name = "(firstname,

MySQL ON DUPLICATE KEY UPDATE with all variables

匆匆过客 提交于 2019-12-11 22:07:48
问题 I have below MySQL insert statement which I want to update all the fields(except for the primary key) if a record exists. Because there are up to 80 columns, I don't want to write the key and value pairs one by one. Could anyone help on this? Below is my code: # Save item to database def process_item(self, item, _): with self.conn.cursor() as c: c.execute(self.query(item), item.values()) def query(self, item): return "INSERT INTO income_statement ({columns}) VALUES ({values}) ON DUPLICATE KEY