sql-update

Using return value from DELETE for UPDATE in Postgres

你离开我真会死。 提交于 2019-12-20 20:31:10
问题 I need to update a table using a value deleted from another table. The situation is a comment vote scorekeeper similar to the one on SO. I'm using python to work the postgres, but that shouldn't make a difference. query=""" UPDATE comment SET score=score-(DELETE FROM history WHERE commentId=%(commentId)s AND userIdentity=%(userIdentity)s RETURNING vote) WHERE commentId=%(commentId)s; """ cursor.execute(query, data) The error arises at (DELETE FROM ; a syntax error arises. I can replace the

generate update query using django orm

喜夏-厌秋 提交于 2019-12-20 20:02:07
问题 I need to implement this query using django orm: update table set field=field+1 where id=id I don't whant to use this: o = model.objects.get(id=id) o.field+=1 o.save() because it use select and when update, and not thread safe. How to implement this via orm? 回答1: Both the previous answerers have part of the solution: you should use update in conjunction with F() : Model.objects.filter(id=id).update(field=F('field') +1)) Note this does an in-place UPDATE without any need for SELECT at all. 回答2

SQL Stored Procedure: If variable is not null, update statement

泄露秘密 提交于 2019-12-20 11:19:43
问题 I have an update statement in a stored procedure that looks generally like this: Update [TABLE_NAME] Set XYZ=@ABC Is there a good way to only trigger the update statement if the variable is not null or the value -1? Similar to an IF NOT EXISTS...INSERT question. Thank you so much. 回答1: Use a T-SQL IF : IF @ABC IS NOT NULL AND @ABC != -1 UPDATE [TABLE_NAME] SET XYZ=@ABC Take a look at the MSDN docs. 回答2: Another approach when you have many updates would be to use COALESCE: UPDATE [DATABASE].

Don't update column if update value is null

不问归期 提交于 2019-12-20 10:59:17
问题 I have a query like this (in a function): UPDATE some_table SET column_1 = param_1, column_2 = param_2, column_3 = param_3, column_4 = param_4, column_5 = param_5 WHERE id = some_id; Where param_x is a parameter of my function. Is there a way to NOT update those columns, for which the param is NULL ? For example - if param_4 and param_5 are NULL , then update only the first three columns and leave old values for column_4 and column_5 . The way I am doing it now is: SELECT * INTO temp_row FROM

Don't update column if update value is null

旧时模样 提交于 2019-12-20 10:59:07
问题 I have a query like this (in a function): UPDATE some_table SET column_1 = param_1, column_2 = param_2, column_3 = param_3, column_4 = param_4, column_5 = param_5 WHERE id = some_id; Where param_x is a parameter of my function. Is there a way to NOT update those columns, for which the param is NULL ? For example - if param_4 and param_5 are NULL , then update only the first three columns and leave old values for column_4 and column_5 . The way I am doing it now is: SELECT * INTO temp_row FROM

MySQL: Creating a new table with information from a query

Deadly 提交于 2019-12-20 10:22:28
问题 In MySQL, I would like to create a new table with all the information in this query: select * into consultaa2 from SELECT CONCAT( 'UPDATE customers SET customers_default_address_id= ', (SELECT a.address_book_id FROM address_book a where c.customers_id=a.customers_id order by address_book_id desc limit 1), ' WHERE customers_id = ', customers_id, ';') AS sql_statement FROM customers c where c.customers_id > 3894; The query is too long for the browser to show the concat and I need this to make

Inner Join Nested Inside Update Statement SQL

回眸只為那壹抹淺笑 提交于 2019-12-20 06:09:43
问题 I am trying to write a query that performs an inner join within an update statement. Here is the current query I am working with: UPDATE singulation1.`q096_cq33r08-ds01-n-testtable` SET visual = t2.visual, inspection_status = t2.inspection_status, inspector_name = t2.inspector_name FROM singulation1.`q096_cq33r08-ds01-n-testtable` t1 INNER JOIN singulation1.`q014_bq31t05-dw30-x` t2 ON (t1.print = t2.print AND t1.id3 = t2.id3); Something about MySql does not like the FROM clause. 回答1: For

Update Existing Access Records from CSV Import , native to MS Access or in VB.NET

风流意气都作罢 提交于 2019-12-20 04:56:22
问题 I am the application administrator for a ticketing system at my organization. We're adding a new client, and need to import their customer records into our system. However, we have been denied access to simply grab these records from a direct database connection. We are limited to entering their ticketing system, and running an export of the existing records to a CSV file. In order to make this work with our system, I'm taking these CSV's and entering them into an MS Access database, which

Using PHP to remove a selected item from drop down, amongst others

别等时光非礼了梦想. 提交于 2019-12-20 04:11:33
问题 Hiya: I'm trying to use PHP to remove selected item in a drop down menu, but all to know avail. Also, when I click on the same submit button that I'll use to remove selected list, I want to UPDATE the MySQL database. I tried using JavaScript, but couldn't use both languages on that submit input button. This is my code for creating the drop down menu and button $opts = array("guns", "knives", "ammo"); $selected = array($_POST['selectMenu']); $revisedOpts = array_diff($opts, $selected); HTML

Update multiple columns that start with a specific string

我与影子孤独终老i 提交于 2019-12-20 03:33:08
问题 I am trying to update a bunch of columns in a DB for testing purposes of a feature. I have a table that is built with hibernate so all of the columns that are created for an embedded entity begin with the same name. I.e. contact_info_address_street1 , contact_info_address_street2 , etc. I am trying to figure out if there is a way to do something to the affect of: UPDATE table SET contact_info_address_* = null; If not, I know I can do it the long way, just looking for a way to help myself out