Questions tagged [sql-update]

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

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

The basic syntax is:

UPDATE table 
SET 
    Column1 = 'x',
    Column2 = 'y'
WHERE id=1

Tagging Recommendation

This tag should be used for general SQL programming language questions, in addition to tags for specific products. For example, questions about Microsoft SQL Server should use the tag, while questions regarding MySQL should use the tag. Tagging by product (including version, e.g , ) is the easiest way to know what functionality is available for the task at hand.

References

12009 questions
267
votes
3 answers

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 work for the purpose but the else condition scan…
amsbarry
  • 2,773
  • 2
  • 13
  • 6
238
votes
11 answers

How to perform update operations on columns of type JSONB in Postgres 9.4

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:…
jvous
  • 2,383
  • 2
  • 12
  • 4
221
votes
12 answers

Update multiple columns in SQL

Is there a way to update multiple columns in SQL server the same way an insert statement is used? Something like: Update table1 set (a,b,c,d,e,f,g,h,i,j,k)= (t2.a,t2.b,t2.c,t2.d,t2.e,t2.f,t2.g,t2.h,t2.i,t2.j,t2.k) from table2 t2 where…
Joe
  • 2,675
  • 3
  • 21
  • 26
218
votes
8 answers

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 into 1 query: UPDATE table_users SET cod_user =…
franvergara66
  • 10,524
  • 20
  • 59
  • 101
204
votes
8 answers

Update some specific field of an entity in android Room

I am using android room persistence library for my new project. I want to update some field of table. I have tried like in my Dao - // Method 1: @Dao public interface TourDao { @Update int updateTour(Tour tour); } But when I try to update…
Rasel
  • 5,488
  • 3
  • 30
  • 39
194
votes
6 answers

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 do I need to return the item first? I read the…
chaonextdoor
  • 5,019
  • 15
  • 44
  • 61
188
votes
6 answers

Number of rows affected by an UPDATE in PL/SQL

I have a PL/SQL function (running on Oracle 10g) in which I update some rows. Is there a way to find out how many rows were affected by the UPDATE? When executing the query manually it tells me how many rows were affected, I want to get that number…
Thomas Lötzer
  • 24,832
  • 16
  • 69
  • 55
188
votes
5 answers

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.id WHERE T2.id IS NULL The MySQL manual states…
Paul Oyster
  • 1,966
  • 2
  • 12
  • 5
186
votes
14 answers

How to update column with null value

I am using mysql and need to update a column with a null value. I have tried this many different ways and the best I have gotten is an empty string. Is there a special syntax to do this?
jim
  • 1,863
  • 2
  • 11
  • 4
182
votes
7 answers

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 a specific ID in B with their data from A for all…
Nir
  • 2,051
  • 2
  • 14
  • 6
180
votes
10 answers

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 = '".$userid."' "); The $points variable is the user's points right now. I…
Karem
  • 17,615
  • 72
  • 178
  • 278
171
votes
8 answers

Change One Cell's Data in mysql

How can I change the data in only one cell of a mysql table. I have problem with UPDATE because it makes all the parameters in a column change but I want only one changed. How?
kasrsf
  • 2,047
  • 4
  • 16
  • 10
166
votes
7 answers

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 single query?
Adamski
  • 5,769
  • 9
  • 32
  • 32
161
votes
11 answers

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 RS_NOM 1 qsdf 2 …
user609511
  • 4,091
  • 12
  • 54
  • 86
154
votes
8 answers

how to increment integer Columns value by 1 in SQL

My questions is how to increment a column's value by 1. For example, suppose a column ID has values 1,2,3,4, .. Now when I update this table then ID column should increment by 1, Now ID will become 2,3,4,5, ..
Varinder
  • 1,780
  • 2
  • 11
  • 18