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
77
votes
2 answers

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 joining 3 tables (set_colors, school_art,…
GGcupie
  • 1,003
  • 1
  • 8
  • 11
77
votes
7 answers

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 BRO1011 5 123456 COOL938 ... …
verheesj
  • 1,438
  • 2
  • 16
  • 24
73
votes
5 answers

Bulk update mysql with where statement

How to update mysql data in bulk ? How to define something like this : UPDATE `table` WHERE `column1` = somevalues SET `column2` = othervalues with somevalues like : VALUES ('160009'), ('160010'), ('160011'); and othervalues…
user3673384
  • 871
  • 2
  • 8
  • 13
69
votes
4 answers

Insert line break in postgresql when updating text field

I am trying to update a text field in a table of my postgresql database. UPDATE public.table SET long_text = 'First Line' + CHAR(10) + 'Second line.' WHERE id = 19; My intended result is that the cell will look like this: First Line Second…
alexyes
  • 773
  • 2
  • 7
  • 14
68
votes
2 answers

Update MySQL with if condition

It seems I have big problems with conditional queries. I have to do a conditional update. I write here what I would like to do: IF(SELECT tipo FROM abbonamento WHERE idU = 17) = 'punti' THEN UDPATE abbonamento SET punti = punti - 1 ELSE …
Martina
  • 1,852
  • 8
  • 41
  • 78
67
votes
4 answers

Don't update column if update value is null

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…
Przemek
  • 6,300
  • 12
  • 44
  • 61
66
votes
3 answers

SQL Server Update Group by

I'm trying to execute this on MS-SQL but returns me an error just at the Group by line update #temp Set Dos=Count(1) From Temp_Table2010 s where Id=s.Total and s.total in (Select Id from #temp) group by s.Total Do anyone knows how can I solve this…
Gerardo Abdo
  • 1,150
  • 3
  • 10
  • 16
64
votes
3 answers

PostgreSQL rename attribute in jsonb field

In postgresql 9.5, is there a way to rename an attribute in a jsonb field? For example: { "nme" : "test" } should be renamed to { "name" : "test"}
T. Kong
  • 643
  • 1
  • 5
  • 5
64
votes
3 answers

Find out the history of SQL queries

An update SQL query was executed on the server, which caused many problems later. How can I get the list of update queries executed in last 2 months, so that I can trace the exact problematic SQL query?
sumit vedi
  • 757
  • 3
  • 11
  • 17
63
votes
4 answers

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 most intuitive to read. However, this results in…
Aaron Silverman
  • 22,070
  • 21
  • 83
  • 103
63
votes
5 answers

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 (status=10). A message with a higher priority…
Toad
  • 15,593
  • 16
  • 82
  • 128
62
votes
10 answers

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 run a VACUUM and ANALYZE commands on that table…
Ricardo
  • 7,785
  • 8
  • 40
  • 60
60
votes
7 answers

SQL Update to the SUM of its joined values

I'm trying to update a field in the database to the sum of its joined values: UPDATE P SET extrasPrice = SUM(E.price) FROM dbo.BookingPitchExtras AS E INNER JOIN dbo.BookingPitches AS P ON E.pitchID = P.ID AND P.bookingID = 1 WHERE E.[required]…
Mark Clancy
  • 7,831
  • 8
  • 43
  • 49
56
votes
7 answers

Insert the same fixed value into multiple rows

I've got a table with a column, lets call it table_column that is currently null for all rows of the table. I'd like to insert the value "test" into that column for all rows. Can someone give me the SQL for this? I've tried INSERT INTO table…
TheDelChop
  • 7,938
  • 4
  • 48
  • 70
53
votes
3 answers

mongoose difference of findOneAndUpdate and update

What is the difference between findOneAndUpdate and update? Both accept criteria to query and doc to update.
SooCheng Koh
  • 2,271
  • 3
  • 21
  • 34