mysql - insert results of query into same table with joins - mysql

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.

Related

Update row as copy of a row in another table without set

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.

Overwriting content from one table to existing table

I have two different tables with two colums that have the same name, datatype and size.
My issue:
When I try to copy the content from one column to the other,
update Table1
set Column4 = (select Column1 from Table2);
I get an error. (subquery returns more than one row)
My question:
Is there a way for me to copy the content from Table2 to Table1 in a similar fashion as the code shows above?
Use Insert ... Select
Insert INTO Table1 (Column4)
SELECT Column1 FROM Table2

Mysql how to do this without locking

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.

updating multiple blank fields in one table with data from another table

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.

Inserting data from one column from one table to another specific table column while ignoring duplicates

I want to insert column1 from table1 to column1 in table2. If the value at column1 in table2 already exists I don't want it to insert it.
Though, I found a question on here that is similar but with all the table columns/rows instead of just one, plus both tables have different schemas except for column1. Because of this, I thought this question would still be valid to post for a more specific answer for mysql newbies like me.
Insert Table2( Column1 )
Select Column1
From Table1 As T1
Where Not Exists (
Select 1
From Table2 As T2
Where T2.Column1 = T1.Column1
)