sql-update

Select sum until a set amount and then update fields in mysql database

*爱你&永不变心* 提交于 2021-01-07 03:23:16
问题 item_id rate status --------- ----------- ------ 1 12 credit 2 10 credit 3 10 credit 4 20 cash 5 55 credit I have the above table, A user inputs and amount of 25. Now I want to update the status of the rows having credit as status from credit to cash until the sum of rate is 25, so in the above table the top 1 rows having a sum of 22 should get a status of cash. Since the user input is 25, I still have a balance of 3 (25-22), this balance should be deducted from the third row making the third

MariaDB/MySQL UPDATE statement with multiple joins including a ranged join

若如初见. 提交于 2021-01-07 01:36:12
问题 I have for tables A Login History create table login_history ( id int auto_increment primary key, ip int unsigned, created datetime(6) not null, uid int unsigned not null, ); An IP To Location Table create table ip2location ( ip_from int unsigned not null primary key, ip_to int unsigned null, country_code char(2) null, ) An Account Table create table account ( uid int unsigned not null primary key, ); Some Orders create table order ( id int auto_increment primary key, item_id varchar(20) not

MariaDB/MySQL UPDATE statement with multiple joins including a ranged join

假装没事ソ 提交于 2021-01-07 01:30:53
问题 I have for tables A Login History create table login_history ( id int auto_increment primary key, ip int unsigned, created datetime(6) not null, uid int unsigned not null, ); An IP To Location Table create table ip2location ( ip_from int unsigned not null primary key, ip_to int unsigned null, country_code char(2) null, ) An Account Table create table account ( uid int unsigned not null primary key, ); Some Orders create table order ( id int auto_increment primary key, item_id varchar(20) not

In R connected to an Access Database through ODBC, how can I update a text field?

左心房为你撑大大i 提交于 2021-01-05 07:22:05
问题 I have R linked to an Access database using the ODBC and DBI packages. I scoured the internet and couldn't find a way to write an update query, so I'm using the dbSendStatement function to update entries individually. Combined with a for loop, this effectively works like an update query with one snag - when I try to update any field in the database that is text I get an error that says "[Microsoft][ODBC Microsoft Access Driver] One of your parameters is invalid." DBI::dbSendStatement(conn =

In R connected to an Access Database through ODBC, how can I update a text field?

时光毁灭记忆、已成空白 提交于 2021-01-05 07:18:44
问题 I have R linked to an Access database using the ODBC and DBI packages. I scoured the internet and couldn't find a way to write an update query, so I'm using the dbSendStatement function to update entries individually. Combined with a for loop, this effectively works like an update query with one snag - when I try to update any field in the database that is text I get an error that says "[Microsoft][ODBC Microsoft Access Driver] One of your parameters is invalid." DBI::dbSendStatement(conn =

Swapping column values in PostgreSQL

孤街浪徒 提交于 2021-01-05 05:48:13
问题 In my PostgreSQL database, I have a table with two text values, t1 and t2 : | id | t1 | t2 | | 1 | abcd | xyz | | 2 | aazz | rst | | 3 | fgh | qwerty | I would like to swap the values of the columns t1 and t2 for every row in the table in a way that, using the above example, this would be the result: | id | t1 | t2 | | 1 | xyz | abcd | | 2 | rst | aazz | | 3 | qwerty | fgh | Also, let's suppose the values from all rows with id=4 onwards (4, 5, 6...) are already correct, is it possible to

Swapping column values in PostgreSQL

十年热恋 提交于 2021-01-05 05:46:33
问题 In my PostgreSQL database, I have a table with two text values, t1 and t2 : | id | t1 | t2 | | 1 | abcd | xyz | | 2 | aazz | rst | | 3 | fgh | qwerty | I would like to swap the values of the columns t1 and t2 for every row in the table in a way that, using the above example, this would be the result: | id | t1 | t2 | | 1 | xyz | abcd | | 2 | rst | aazz | | 3 | qwerty | fgh | Also, let's suppose the values from all rows with id=4 onwards (4, 5, 6...) are already correct, is it possible to

Update SQL with Aliased tables still returns “table is ambiguous” error

筅森魡賤 提交于 2020-12-31 04:38:36
问题 I am trying to run the below update but running into the "table is ambiguous" error. UPDATE dbo.cg SET cg.column = gId.ID FROM dbo.a INNER JOIN dbo.cg as cId ON cId.[a] = dbo.a.[c] INNER JOIN dbo.cg as gId ON gId.[a] = dbo.a.[b]; The table dbo.a contains data to update a value in cg based on a relationship to same table against a value in a different column. It is a self-referencing hierarchy. As you can see, everything is aliased so I am a bit confused why this won't run. Many thanks in

Return updated row attributes on UPDATE

时光毁灭记忆、已成空白 提交于 2020-12-29 05:31:05
问题 My query is as follow: UPDATE t1 SET t1.foreign_key = (SELECT id FROM t2 WHERE t2.col = %s ) WHERE t1.col = %s How do I return some attributes of the updated row in the table in the same query? 回答1: Use the RETURNING clause. The optional RETURNING clause causes UPDATE to compute and return value(s) based on each row actually updated. Any expression using the table's columns, and/or columns of other tables mentioned in FROM , can be computed. The new (post-update) values of the table's columns

Return updated row attributes on UPDATE

只愿长相守 提交于 2020-12-29 05:30:37
问题 My query is as follow: UPDATE t1 SET t1.foreign_key = (SELECT id FROM t2 WHERE t2.col = %s ) WHERE t1.col = %s How do I return some attributes of the updated row in the table in the same query? 回答1: Use the RETURNING clause. The optional RETURNING clause causes UPDATE to compute and return value(s) based on each row actually updated. Any expression using the table's columns, and/or columns of other tables mentioned in FROM , can be computed. The new (post-update) values of the table's columns