Can I combine two queries like this
first: UPDATE table SET col1=1 WHERE id='x';
second: UPDATE table SET col1=0 WHERE id='y';
can I join these queries in one?
UPDATE table
SET col1 = CASE id WHEN 'x' THEN 1 ELSE 0 END
WHERE id IN ('x','y')
Use this sql query:
UPDATE table
SET col1 =
CASE id
WHEN 'x' THEN 1
WHEN 'y' THEN 0
END
WHERE id IN ('x','y');
Also see Multiple Updates in MySQL
UPDATE table
SET col1 = (id = 'x')
WHERE id IN ('x','y');
Related
I have a table named as mytable where it has column named a_column.currently it is null valued and it has 100 rows. I just need to know how to set the value of first 50 rows as ABC and rest 50 rows as XYZ. I have tried to use below queries but i was unsuccessful. kindly suggest me the query.
UPDATE mytable
SET a_column= 'ABC';
INSERT INTO table (mytable) VALUES ("ABC/XYZ")
chamath
You can use following query to do that.
UPDATE mytable
SET a_column= (CASE WHEN id > 50 THEN 'XYZ' ELSE 'ABC' END);
i have a table in my MySQL database which i have added a new column to.
I would like to update this column on every row with a number starting at 20000 going up +1 each time.
i have tried this solution:
UPDATE table1 set new_col = new_col + 1;
but it just updates all rows with the same number
The easy way:
UPDATE table1 t, (SELECT #nr:= 20000-1) tmp
SET t.new_col = (#nr:=#nr+1) ;
I have used this query to solve this:
SET #rank:=20000;
update customer
set accountnumber_new=#rank:=#rank+1
I'm trying to convert 0's and 1's in my table 'collection' to '0' being 'no' and '1' being 'yes' within SQL Server, I've researched various places and i still haven't found a concrete answer. Any help would be greatly appreciated
select
case
when Yourfield = 0 then 'no'
when Yourfield = 1 then 'yes'
end as GotIt
from
collection
Create another field and update it with your new values.
ALTER TABLE table1 ADD newfield VARCHAR(10);
UPDATE TABLE1 SET newfield = if(oldfield = 1, 'YES','NO');
ALTER TABLE table1 drop field oldfield;
alter table table1 rename newfield to oldfield;
I'm trying to update multiple rows in a table but due to server resources being limited I can't do it in individual UPDATE statements. I know that this can be done in a single statement by using a CASE but I'm having trouble getting this to work as the table needs to be updated using a composite key (three columns) rather than a single value.
This example works:
UPDATE myTable
SET newValue = CASE id
WHEN 1 THEN 'val1'
WHEN 2 THEN 'val2'
WHEN 3 THEN 'val3'
END,
WHERE id IN (1,2,3)
But I need something like this to work:
UPDATE myTable
SET newValue = CASE (id,x,y)
WHEN (1,1,1) THEN 'val1'
WHEN (1,1,2) THEN 'val2'
WHEN (1,1,3) THEN 'val3'
END,
WHERE id IN (1,2,3)
Is there a way to have something like this in a single UPDATE statement or am I going to have to work around the limited server resources?
Slightly refactored from your example:
UPDATE myTable
SET newValue = CASE
WHEN id=1 AND x=1 AND y=1 THEN 'val1'
WHEN id=1 AND x=1 AND y=2 THEN 'val2'
WHEN id=1 AND x=1 AND y=3 THEN 'val3'
END
WHERE id IN (1,2,3)
You could use MySQL's "REPLACE":
REPLACE INTO myTable (id,x,y,newValue)
VALUES (1,1,1,'val1'),
(1,1,2,'val2'),
(1,1,3,'val3');
I have 100 columns in my table and I want to update only columns that have NULL value.
I am comparing master table columns with temp table and trying to update master table column value with temp table column value.
Please help me in this regards.
Thanks in advance
Something like:
Update t1
set field1 = coalesce(t1.field1, 'test')
, field2 = coalesce(t1.field2, t1.field1)
, field3 = coalesce(t1.field3, t2.field1)
, field4 = coalesce(t1.field4, t2.field1, t2.field3)
FROM table1 t1
join table2 t2
on t1.someid = t2.someId
I have given you three examples of differnt ways you might update if the field is null. The first shows how to set it to a text value, the second how to set it to another field in the same table and third is the one where you get the value from a different table. The forth shows what to do if the value you are setting it to is also nul and thus want to use still another value in its place. You will need to write a coalesce update for each of the 100 columns.
The following should update a row in the master table with either its previous value (if it is not null) or the value from the temp table if the value of the master table is null.
This requires that master and temp table can be joined on a key field named Key in my case.
UPDATE m
SET
m.field1 = ISNULL(m.field1, t.field1),
...
FROM
MasterTable m
INNER JOIN TempTable t ON t.Key = m.Key
UPDATE B SET B.value = T.value
FROM
tblMaster B
INNER JOIN tblTemp T ON B.ID = T.ID
WHERE B.value IS NULL