user3686909
user3686909

Reputation: 45

SQL query update with two WHERE

I's posible update table with two where?

UPDATE table_name
SET column_name ='test' 
WHERE code='605';

Becouse sql syntax with two WHERE not exist? How to solve?

Upvotes: 0

Views: 78

Answers (2)

Raging Bull
Raging Bull

Reputation: 18747

Use AND or OR:

Using AND:

UPDATE table_name
SET column_name ='test' 
WHERE code='605'
  AND Col='someval'

When you use AND, it will update the table only if both conditions are satisfied.

Using OR:

UPDATE table_name
SET column_name ='test' 
WHERE code='605'
  OR Col='someval'

When you use OR, it will update the table if any one of the conditions are satisfied.

EDIT:

For joining another table in UPDATE query:

UPDATE T1
SET column_name='test'
FROM table_name T1 JOIN
     another_table T2 on T1.PK=T2.FK
WHERE T1.code='605' 
  AND T2.Column_from_other_table='someval'

Upvotes: 3

user3693611
user3693611

Reputation:

You can add some expressions to your where clause:

UPDATE table_name
SET column_name ='test' 
WHERE code='605' or code = '907' or code = '534';

That query will update all rows with that codes.

Upvotes: 2

Related Questions