Update single column in a table with with multiple conditions in the WHERE statement
I think you would be better off just using a join clause:
UPDATE table_new
SET col_4 = 'new value'
FROM table_new tn
LEFT JOIN table_old to ON tn.col_1 = to.col_1
AND tn.col_2 = to.col_2
AND tn.col_3 = to.col_3
WHERE to.col_1 IS NULL;
the left join will give you matching/non matching records, and you can then determine where the records from the old table aren't in the new table by looking for the null fields in the right side (table_old) of those results. If you need it to be more specific, you could add IS NULL statements in the WHERE clause for all the column names.
Update a table with data from other table with multiple conditions?
I suspect that you really want EXISTS
-- that is to set all values in table A
, with 1
if there is a non-NULL
matching event. That would be:
UPDATE A
SET NEWCOLUMN = (CASE WHEN EXISTS (SELECT 1
FROM B
WHERE b.ARTICLENUMBER = a.ARTICLENUMBER AND
b.EVENT IS NOT NULL
)
THEN 1 ELSE 0
END);
Note that this updates all rows in A
-- even those with no matching article in B
. As I say, I think this is what you want to do, although it is not exactly how your question is phrased. Your question does not specify what to do for ARTICLENUMBER
s that are not in B
.
Multiple condition update statement in SQL Server
You only need a single case statement. I used = versus in since you aren't supplying multiple values.
update table
set own=case
when own = '1' then '10'
when own = '2' then '20'
when own = '3' then '30'
....
else '00'
End
SQL updating table with multiple conditions in the where statement ACCESS
If you want to do this in a single update, you'll need conditional logic:
Update table1
SET Letter1 = IIF(Letter1 = "X", "_", Letter1),
Letter2 = IIF(Letter1 = "X", "_", Letter2)
WHERE Letter1 = "X" OR Letter2 = "X";
However, two separate statements might be clearer:
Update table1
SET Letter1 = "_"
WHERE Letter1 = "X";
Update table1
SET Letter2 = "_"
WHERE Letter2 = "X";
PostgreSQL UPDATE statement with CASE and multiple conditions
Try this
UPDATE "myDatabase".myTable
SET transpondertype=(CASE
WHEN transpond=0 THEN 'N'
WHEN transpond=1 THEN
CASE WHEN modesequip=1
THEN 'S'
ELSE 'A'
END
END);
Update from another table with multiple condition
A common column is needed from TABLE_C
with the others for matching condition, that seems to be mat_id
with TABLE_B
.
A subquery needed, which should include the NULL vs. NOT NULL cases ( NVL(a.ch_dt, a.mod_dat)
) , to be used to bring the counterpart value for ch_dd_dt
column, and after EXISTS
clause.
Therefore I constructed such a query :
UPDATE TABLE_C c
SET c.ch_dd_dt =
(SELECT NVL(a.ch_dt, a.mod_dat)
FROM TABLE_A a
JOIN TABLE_B b
ON b.cl_no = a.cl_no
WHERE a.ch_dt IS NULL
AND b.mat_id = c.mat_id)
WHERE EXISTS (SELECT NVL(a.ch_dt, a.mod_dat)
FROM TABLE_A a
JOIN TABLE_B b
ON b.cl_no = a.cl_no
WHERE a.ch_dt IS NULL
AND b.mat_id = c.mat_id)
which only updates the row of TABLE_C
with mat_id = '04'
Demo
Alternatively, you can use a MERGE
Statement including MATCHED
case only :
MERGE INTO TABLE_C c
USING
(SELECT NVL(a.ch_dt, a.mod_dat) AS ch_dt, b.mat_id
FROM TABLE_A a
JOIN TABLE_B b
ON b.cl_no = a.cl_no
WHERE a.ch_dt IS NULL) ab
ON ( ab.mat_id = c.mat_id)
WHEN MATCHED THEN UPDATE SET c.ch_dd_dt = ab.ch_dt
Demo
Related Topics
Oracle Pls-00363: Expression '' Cannot Be Used as an Assignment Target
Cross Apply Performance Difference
Sql Server Begin/End Vs Begin Trans/Commit/Rollback
Atomically Set Serial Value When Committing Transaction
MySQL Join Two Tables Count and Sum from Second Table
Design Option for 'Recurring Tasks'
Sql Server Server Query - Count Distinct Datetime Field
How to Add a "Custom" Row to The Top of a Select Result Set
Sql Server 2008 - How to Convert Gmt(Utc) Datetime to Local Datetime
Replacing Only The Nth Instance of Character
Update Multiple Records in Sql