Questions tagged [sql-update]

An SQL UPDATE statement is used to change existing rows in a table.

sql-update
Filter by
Sorted by
Tagged with
4266 votes
40 answers
5.3m views

How do I UPDATE from a SELECT in SQL Server?

In SQL Server, it is possible to insert rows into a table with an INSERT.. SELECT statement: INSERT INTO Table (col1, col2, col3) SELECT col1, col2, col3 FROM other_table WHERE sql = 'cool' Is it ...
jamesmhaley's user avatar
  • 44.9k
1627 votes
19 answers
2.2m views

How can I do an UPDATE statement with JOIN in SQL Server?

I need to update this table in SQL Server with data from its 'parent' table, see below: Table: sale id (int) udid (int) assid (int) Table: ud id (int) assid (int) sale.assid contains the ...
Ant Swift's user avatar
  • 20.6k
583 votes
5 answers
1.3m views

How do I (or can I) SELECT DISTINCT on multiple columns?

I need to retrieve all rows from a table where 2 columns combined are all different. So I want all the sales that do not have any other sales that happened on the same day for the same price. The ...
sheats's user avatar
  • 33.9k
1209 votes
29 answers
2.6m views

SQL Update from One Table to Another Based on a ID Match

I have a database with account numbers and card numbers. I match these to a file to update any card numbers to the account number so that I am only working with account numbers. I created a view ...
user avatar
982 votes
13 answers
1.1m views

Update a table using JOIN in SQL Server?

I want to update a column in a table making a join on other table e.g.: UPDATE table1 a INNER JOIN table2 b ON a.commonfield = b.[common field] SET a.CalculatedColumn= b.[Calculated Column] WHERE ...
Manjot's user avatar
  • 11.4k
686 votes
12 answers
1.5m views

MySQL - UPDATE query based on SELECT Query

I need to check (from the same table) if there is an association between two events based on date-time. One set of data will contain the ending date-time of certain events and the other set of data ...
John M's user avatar
  • 14.5k
813 votes
13 answers
1.1m views

SQL update query using joins

I have to update a field with a value which is returned by a join of 3 tables. Example: select im.itemid ,im.sku as iSku ,gm.SKU as GSKU ,mm.ManufacturerId as ManuId ,mm....
Shyju's user avatar
  • 217k
330 votes
8 answers
1.4m views

Update a table with data from another table

Table 1: id name desc ----------------------- 1 a abc 2 b def 3 c adf Table 2: id name desc ----------------------- 1 x 123 2 y 345 In ...
Muhd's user avatar
  • 25k
449 votes
20 answers
501k views

Multiple Updates in MySQL

I know that you can insert multiple rows at once, is there a way to update multiple rows at once (as in, in one query) in MySQL? Edit: For example I have the following Name id Col1 Col2 Row1 1 ...
Teifion's user avatar
  • 110k
170 votes
7 answers
345k views

MySQL, update multiple tables with one query

I have a function that updates three tables, but I use three queries to perform this. I wish to use a more convenient approach for good practice. How can I update multiple tables in MySQL with a ...
Adamski's user avatar
  • 5,909
1200 votes
23 answers
3.0m views

MySQL error code: 1175 during UPDATE in MySQL Workbench

I want to update the column visited to give it the value 1. I tried this command in the SQL editor inside MySQL workbench: UPDATE tablename SET columnname=1; But I get an error that says: You are ...
Jury A's user avatar
  • 19.6k
79 votes
7 answers
107k views

MySQL update table based on another tables value

I have a two tables, Here is my first table, ID SUBST_ID CREATED_ID 1 031938 TEST123 2 930111 COOL123 3 000391 THIS109 4 039301 ...
verheesj's user avatar
  • 1,458
228 votes
9 answers
502k views

UPDATE multiple rows with different values in one query in MySQL

I am trying to understand how to UPDATE multiple rows with different values and I just don't get it. The solution is everywhere but to me it looks difficult to understand. For instance, three updates ...
franvergara66's user avatar
329 votes
7 answers
551k views

mysql update column with value from another table

I have two tables, both looking like id name value =================== 1 Joe 22 2 Derk 30 I need to copy the value of value from tableA to tableB based on check name in each table. Any ...
LeoSam's user avatar
  • 4,871
95 votes
2 answers
56k views

How do I make an UPDATE while joining tables on SQLite?

I tried : UPDATE closure JOIN item ON ( item_id = id ) SET checked = 0 WHERE ancestor_id = 1 And: UPDATE closure, item SET checked = 0 WHERE ancestor_id = 1 AND item_id = id Both works with ...
Bite code's user avatar
  • 588k
488 votes
13 answers
1.2m views

How to insert a value that contains an apostrophe (single quote)?

What is the correct SQL syntax to insert a value with an apostrophe in it? Insert into Person (First, Last) Values 'Joe', 'O'Brien' I keep getting an error as I think the apostrophe after the ...
leora's user avatar
  • 193k
245 votes
11 answers
332k views

How to perform update operations on columns of type JSONB

Looking through the documentation for the Postgres 9.4 datatype JSONB, it is not immediately obvious to me how to do updates on JSONB columns. Documentation for JSONB types and functions: http://www....
jvous's user avatar
  • 2,453
189 votes
5 answers
310k views

UPDATE multiple tables in MySQL using LEFT JOIN

I have two tables, and want to update fields in T1 for all rows in a LEFT JOIN. For an easy example, update all rows of the following result-set: SELECT T1.* FROM T1 LEFT JOIN T2 ON T1.id = T2....
Paul Oyster's user avatar
  • 1,986
112 votes
11 answers
337k views

MySQL - UPDATE query with LIMIT

I want to update rows in my table with starting from 1001 to next 1000. I tried with following query: UPDATE `oltp_db`.`users` SET p_id = 3 LIMIT 1001, 1000 This is giving me syntax error. Is this ...
Rahul Shelke's user avatar
  • 2,116
163 votes
11 answers
299k views

SQL Update with row_number()

I want to update my column CODE_DEST with an incremental number. I have: CODE_DEST RS_NOM null qsdf null sdfqsdfqsdf null qsdfqsdf I would like to update it to be: CODE_DEST ...
user609511's user avatar
  • 4,151
78 votes
4 answers
58k views

Return pre-UPDATE column values using SQL only

I posted a related question, but this is another part of my puzzle. I would like to get the OLD value of a column from a row that was UPDATEd - WITHOUT using triggers (nor stored procedures, nor any ...
pythonlarry's user avatar
  • 2,326
547 votes
8 answers
704k views

updating table rows in postgres using subquery

I have this table in a postgres 8.4 database: CREATE TABLE public.dummy ( address_id SERIAL, addr1 character(40), addr2 character(40), city character(25), state character(2), zip character(...
stackover's user avatar
  • 6,635
88 votes
9 answers
59k views

How do I do large non-blocking updates in PostgreSQL?

I want to do a large update on a table in PostgreSQL, but I don't need the transactional integrity to be maintained across the entire operation, because I know that the column I'm changing is not ...
user avatar
67 votes
4 answers
93k views

MySQL update a joined table

I want to update a table in a statement that has several joins. While I know the order of joins doesn't really matter (unless you you are using optimizer hints) I ordered them a certain way to be ...
Aaron Silverman's user avatar
39 votes
4 answers
122k views

mysql after insert trigger which updates another table's column

i'm trying to write a trigger, I have following tables: BookingRequest: +-----------+---------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra ...
DeadKennedy's user avatar
29 votes
3 answers
114k views

PHP UPDATE prepared statement

I'm trying to learn the proper way to use prepared statements to avoid SQL injections etc. When I execute the script I get a message from my script saying 0 Rows Inserted, I expect this to say 1 ...
user0129e021939232's user avatar
28 votes
2 answers
48k views

update multiple rows using limit in mysql?

UPDATE messages set test_read =1 WHERE userid='xyz' ORDER BY date_added DESC LIMIT 5, 5 ; I am trying to use this query to update a set of 5 rows using limit but mysql is ...
halocursed's user avatar
  • 2,475
11 votes
3 answers
4k views

Casting NULL type when updating multiple rows

I have a problem when I try to update many rows at the same time. Here is the table and query I use (simplified for better reading): table CREATE TABLE foo ( pkid integer, x integer, y ...
AlbertFerras's user avatar
516 votes
9 answers
610k views

how can I Update top 100 records in sql server

I want to update the top 100 records in SQL Server. I have a table T1 with fields F1 and F2. T1 has 200 records. I want to update the F1 field in the top 100 records. How can I update based on TOP ...
Rajesh's user avatar
  • 6,419
3 votes
6 answers
24k views

SQL update from one Table to another based on a ID match IN db2

The Query below is suited in SQL sErver. But in DB2 it does not give results: Error is SQLCODE = -199, ERROR: ILLEGAL USE OF KEYWORD FROM. Query: UPDATE Sales_Import SET Sales_Import....
Agent Mahone's user avatar
278 votes
3 answers
912k views

I want to use CASE statement to update some records in sql server 2005

UPDATE dbo.TestStudents SET LASTNAME = ( CASE WHEN (LASTNAME = 'AAA') THEN 'BBB' WHEN (LASTNAME = 'CCC') THEN 'DDD' WHEN (LASTNAME = 'EEE') THEN 'FFF' ELSE (LASTNAME) END ) The statement ...
amsbarry's user avatar
  • 2,883
182 votes
10 answers
354k views

Increment value in MySQL update query

I have made this code for giving out +1 point, but it doesn't work properly. mysql_query(" UPDATE member_profile SET points= ' ".$points." ' + 1 WHERE user_id = '".$...
Karem's user avatar
  • 17.9k
146 votes
5 answers
177k views

How to prepend a string to a column value in MySQL?

I need a SQL update statement for updating a particular field of all the rows with a string "test" to be added in the front of the existing value. For example, if the existing value is "try" it ...
santanu's user avatar
  • 5,417
46 votes
3 answers
6k views

SQL UPDATE order of evaluation

What is the order of evaluation in the following query: UPDATE tbl SET q = q + 1, p = q; That is, will "tbl"."p" be set to q or q + 1? Is order of evaluation here governed by SQL standard? Thanks. ...
pilcrow's user avatar
  • 57.5k
42 votes
3 answers
49k views

Python mySQL Update, Working but not updating table

I have a python script which needs to update a mysql database, I have so far: dbb = MySQLdb.connect(host="localhost", user="user", passwd="pass", db="database") try: curb =...
user2144306's user avatar
2 votes
2 answers
6k views

Postgres error updating column data

Trying to run a update script on a table, but getting an error: ERROR: column "ok" does not exist LINE 2: SET first_name="ok", last_name="pk", email="ooo", phone="... CREATE TABLE employee ...
Kyle's user avatar
  • 1,173
195 votes
6 answers
72k views

What's the difference between findAndModify and update in MongoDB?

I'm a little bit confused by the findAndModify method in MongoDB. What's the advantage of it over the update method? For me, it seems that it just returns the item first and then updates it. But why ...
chaonextdoor's user avatar
  • 5,059
77 votes
2 answers
115k views

PHP MYSQL UPDATE if Exist or INSERT if not?

I have no idea if this is even remotely correct. I have a class where I would like to update the database if the fields currently exist or insert if they do not. The complication is that I am doing a ...
GGcupie's user avatar
  • 1,003
63 votes
5 answers
130k views

How to update and order by using ms sql

Ideally I want to do this: UPDATE TOP (10) messages SET status=10 WHERE status=0 ORDER BY priority DESC; In English: I want to get the top 10 available (status=0) messages from the DB and lock them (...
Toad's user avatar
  • 15.8k
32 votes
12 answers
131k views

Table is 'read only'

When I want to execute an update query on my table I got an error saying: 1036 - Table data is read only. How can I fix that? Table attributes in /var/db/mysql are set to 777. 'Repair Table' ...
Cyclone's user avatar
  • 15k
191 votes
7 answers
267k views

SQL update fields of one table from fields of another one

I have two tables: A [ID, column1, column2, column3] B [ID, column1, column2, column3, column4] A will always be subset of B (meaning all columns of A are also in B). I want to update a record with ...
Nir's user avatar
  • 2,141
98 votes
7 answers
198k views

Bulk/batch update/upsert in PostgreSQL

I'm writing a Django-ORM enchancement that attempts to cache models and postpone model saving until the end of the transaction. It's all almost done, however I came across an unexpected difficulty in ...
julx's user avatar
  • 8,870
63 votes
10 answers
93k views

Slow simple update query on PostgreSQL database with 3 million rows

I am trying a simple UPDATE table SET column1 = 0 on a table with about 3 million rows on Postegres 8.4 but it is taking forever to finish. It has been running for more than 10 min. Before, I tried to ...
Ricardo's user avatar
  • 7,855
37 votes
6 answers
73k views

What are differences between INSERT and UPDATE in MySQL?

It seems INSERT and UPDATE do the same things to me. Is there any occasions where I should use INSERT instead of UPDATE and vice versa?
shin's user avatar
  • 32.3k
32 votes
4 answers
53k views

MySql update two tables at once

I have two tables that need the exact same values for denormalization purposes. Here's the query. first table UPDATE Table_One SET win = win+1, streak = streak+1, score = score+200 WHERE userid = 1 ...
user962449's user avatar
  • 3,803
32 votes
1 answer
18k views

Does MySQL overwrite a column of same value on update?

When updating a table in MySQL, for example: Table user user_id | user_name 1 John 2 Joseph 3 Juan If I run the query UPDATE `user` SET user_name = 'John' WHERE user_id = 1 ...
Phiter's user avatar
  • 14.8k
31 votes
5 answers
39k views

Android SQLite Database, WHY drop table and recreate on upgrade

In the tutorials I am following and a lot of more places I see this, onUpgrade -> drop table if exists, then recreate table. What is the purpose of this? private static class DbHelper extends ...
Esqarrouth's user avatar
  • 38.9k
24 votes
6 answers
81k views

MySQL UPDATE and SELECT in one pass

I have a MySQL table of tasks to perform, each row having parameters for a single task. There are many worker apps (possibly on different machines), performing tasks in a loop. The apps access the ...
Paul Oyster's user avatar
  • 1,248
24 votes
4 answers
48k views

MySQL/SQL: Update with correlated subquery from the updated table itself

I have a generic question that I will try to explain using an example. Say I have a table with the fields: "id", "name", "category", "appearances" and "ratio" The idea is that I have several items, ...
Roee Adler's user avatar
  • 33.8k
24 votes
1 answer
22k views

Using jsonb_set() for updating specific jsonb array value

Currently I am working with PostgreSQL 9.5 and try to update a value inside an array of a jsonb field. But I am unable to get the index of the selected value My table just looks like this: CREATE ...
Daniel Seichter's user avatar

1
2 3 4 5
18