I am trying to find all fields in one table that are empty and populate them with data from another table. Here is what I have:
UPDATE t1 SET col1 = (
SELECT col99
FROM t2
WHERE t1.product_ID = t2.product_ID
) WHERE col1 IS NULL
which works perfectly for updating all the blank fields in col1 of t1. But I also need to check for blanks in other fields and update them. I don't want the query to update all the fields each time any one of them is blank, just the blank field. I could run multiple queries but I have to imagine there is a cleaner, better way.
Thanks,
Matthew
Try:
UPDATE t1
SET
col1 = COALESCE(col1, (SELECT col99 FROM t2 WHERE t1.product_ID = t2.product_ID)),
col2 = COALESCE(col2, (SELECT ...)),
-- etc.
Should not perform better than your initial solution. It's just doing the job with only one UPDATE query instead of several.
Related
I want to synchronize multiple tables. I want to copy a row from one table and update it to another table but I donot want to specify the column names. This is because I donot want to hard code the sql query for every table separately.
I have tried several variants, it worked with insert, but I cannot find a solution for it with Update
insert into t2 select * from t1 where sno=2;
In this code, I donot need to pass the columns names to Insert, it automatically knows the columns list. I want to achieve the same with Update where I donot want to pass the columns list inside SET portion.
you always need to use SET and assign columns one by one. However you can use subquery for updating your source table using another table
update
Table1 as T1
inner join (
select *
from Table2
where ...
) as t2 on t1.Id = t2.Id
set T1.something = t2.something
Well, if the columns are the same, you could do:
delete from t2
where exists (select 1 from t1 where t1.id = t2.id);
insert into t2
select *
from t1;
I don't see why update would be used to replace data (although I would still list all the columns).
If the columns are not the same, then you need to list out the ones that change.
I use this site all the time to find answers to my questions but this is the first time I've ask one.
I have two tables and want to query both tables and insert the results into two columns on table 1. Something like this:
SELECT a.column1 from table1 a LEFT Join ( SELECT 'column1' from 'table2' ) AS a ON where a.column1 like '%column1.table2%';
Basically then insert the result into column5 and column6 on table 1
I know that this isn't correct as it doesn't work and it's not going to update any thing. For testing I'm running select statements to verify before running the update command. Another way of saying what I need would be:
If column1 in table1 is like column1 in table2 then update column5 in table1 with corresponding entry from column2 in table2 and update column6 in table1 with column7 from table1 with corresponding entry in column3 from table2;
I realize that this is not the best explanation but that is the best way I can explain what I want. Please ask questions if more information is needed and I will do my best to explain.
Thanks for any input you have.
In MySQL you can do multi-table updates in a single update statement using joins in the table list:
update t1 inner join t2 on t1.column1 like concat('%',t2.column1, '%')
set t1.column5=t2.column2, t1.column6=t2.column7
However, using like is not necessarily the best idea, since more than 1 records from t2 may match the same record within t1, therefore a single t1 record may be updated several times during a single run.
Your description of which t1 fields should be updated by which field from t2 is inaccurate, cannot really tell the logic there.
I want to query one table to see whether there exist any rows with 'A' type.
so I use this sql:
SELECT EXISTS(select * from %T where type = 'A');
then I need to update another table's column value to the above result. In order to prevent an insert with 'A' type happen during update, I am thinking to use a lock. but lock is very expensive, is there other alternative way to do this without locking?
If we have to use lock, I am thinking if table has already had type A, there is no need to lock insert during update because the result will still be 1. only prevent insert when there is no row with type A. How to do that?
Thanks!
Since you want to update a table when there is a type 'A', normally I would suggest you do the update or check every time right after inserting or deleting a row.
Or you could do something like this:
UPDATE table1 t1
LEFT JOIN table2 t2 ON t1.type = t2.type
SET t1.column1 = 1
WHERE t2.type IS NOT NULL
or
UPDATE table1 t1
SET t1.column1 = 1
WHERE EXISTS (
SELECT ...
FROM table2
WHERE type = 'A'
);
Please give me more details such as what is your table like and what exactly you want to do so we can discuss further.
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
I need to update the content of one data field in a table with the content of another field in a table every time two separate fields, one on each table, match up. I've been trying this syntax but I just can't get it to work properly without giving me an error.
UPDATE table1
SET field1 = table2.field1
FROM Table1,Table2
WHERE Table1.entry = Table2.entry
update ... from is sql server's syntax. In MySQL you can just use multiple tables directly:
update
table1 t1
join table2 t2 on t2.field = t1.field
set
t1.field1 = t2.matchingfield
where
t1.whatever = t2.whatever
All is detailed on the MySQL update reference page.