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

How do I tell when a MySQL UPDATE was successful versus actually updated data?

How do I tell when a MySQL UPDATE was successful versus actually updated data? Example: TABLE id city_name 1 Union 2 Marthasville If I run the following: $data = array('city_name', 'Marthasville'); //update record 2 from Marthasville…
zechdc
  • 3,374
  • 9
  • 40
  • 52
36
votes
5 answers

MySQL ON UPDATE CURRENT_TIMESTAMP not updating

I've got a table that looks like this: CREATE TABLE IF NOT EXISTS `Hosts` ( `id` int(128) NOT NULL AUTO_INCREMENT, `IP` varchar(15) NOT NULL DEFAULT '', `Port` varchar(5) NOT NULL DEFAULT '', `Password` varchar(32) NOT NULL DEFAULT '', `Username`…
coding_hero
  • 1,759
  • 3
  • 19
  • 34
35
votes
2 answers

Sql query for updating database if value is not null?

I am having a table which has about 17 fields. I need to perform frequent updates in this table. But the issue is each time I may be updating only a few fields. Whats the best way to write a query for updating in such a scenario? I am looking for an…
Zach
  • 9,989
  • 19
  • 70
  • 107
35
votes
3 answers

MySQL - Update values based on subquery

let's say I have select, which return me from table1: ID Name 1 Bob 2 Alice 3 Joe Then I want UPDATE values in another table based on this result: UPDATE table2 SET Name = table1.Name WHERE ID = table1.ID As I understood, I can only do…
MasterClass
  • 355
  • 1
  • 3
  • 10
34
votes
1 answer

How to update two columns in a MySQL database?

This doesn't work: UPDATE customers SET firstname="John" AND lastname="Smith" WHERE id=1;
Shields
  • 343
  • 1
  • 3
  • 4
34
votes
2 answers

Update a column of a table with a column of another table in PostgreSQL

I want to copy all the values from one column val1 of a table table1 to one column val2 of another table table2. I tried this command in PostgreSQL: update table2 set val2 = (select val1 from table1) But I got this error: ERROR: more than one row…
f.ashouri
  • 5,409
  • 13
  • 44
  • 52
34
votes
1 answer

How to update an SQLite database with a search and replace query?

My SQL knowledge is very limited, specially about SQLite, although I believe this is will be some sort of generic query... Or maybe not because of the search and replace... I have this music database in SQLite that has various fields of course but…
rfgamaral
  • 16,546
  • 57
  • 163
  • 275
33
votes
5 answers

MySQL Error: Incorrect usage of UPDATE and LIMIT

How can I correct this problem so that my MySQL code works correctly. Here is my MySQL code that gives me the problem. $q = "UPDATE users INNER JOIN contact_info ON contact_info.user_id = users.user_id SET active.users = NULL WHERE…
HELP
  • 14,237
  • 22
  • 66
  • 100
33
votes
3 answers

SQL update query syntax with inner join

Can anyone find my error in this query? I'm using SQL Server 2000 and I want to update all entries in the CostEntry table to the corresponding value in the ActiveCostDetails table. The where clause DOES work with a select statement. UPDATE…
MAW74656
  • 3,449
  • 21
  • 71
  • 118
33
votes
5 answers

MySQL UPDATE append data into column

I need to UPDATE tablename (col1name) If there is already data, I need to append it with values 'a,b,c' If it is NULL, I need to add the values 'a,b,c' I know there is a CONCAT argument, but not sure what the SQL syntax would be. update tablename…
Robert Hoffmann
  • 343
  • 1
  • 3
  • 7
33
votes
2 answers

Update Statement using Join and Group By

I have written the below Update Statement, but it shows the error such as "Incorrect syntax near the keyword 'GROUP'." UPDATE J SET J.StatusID = CASE WHEN SUM(DUV.VendorDUQuantity) = SUM(RD.InvoiceQuantity) THEN 1 ELSE J.StatusID…
thevan
  • 10,052
  • 53
  • 137
  • 202
33
votes
3 answers

UPDATE Query without WHERE Clause

Can the UPDATE query be used without a WHERE clause? And if so in what conditions?
heretolearn
  • 6,387
  • 4
  • 30
  • 53
32
votes
4 answers

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 AND lid = 1 LIMIT 1 second table UPDATE…
user962449
  • 3,743
  • 9
  • 38
  • 53
32
votes
1 answer

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 Will MySQL write the same value again or ignore…
Phiter
  • 14,570
  • 14
  • 50
  • 84
32
votes
4 answers

SQL Inner join 2 tables with multiple column conditions and update

I am using this script, trying to join 2 tables with 3 conditions and update T1: Update T1 set T1.Inci = T2.Inci ON T1.Brands = T2.Brands AND T1.Category= T2.Category AND T1.Date = T2.Date but I encounter: Incorrect syntax near the keyword…
marilyn
  • 545
  • 3
  • 12
  • 19