Hi I have two table one like this one:
table1
and one like this:
table2
I would like to update all the fields on the table2 column "newID" based on this rules: if (table2.ID = table1.ID_actual or table2.ID=table1.ID_old) then table2.newID = table1.newID
How can I resolve this problem ?
You need a join of the 2 tables in the UPDATE statement:
UPDATE table2 t2
INNER JOIN table1 t1 ON t2.ID IN (t1.ID_actual, t1.ID_old)
SET t2.newID = t1.newID
Related
I have two MySql Databases.
One has:
___id___|___name___|__date____|
Second database has
___id___|___tag___|
One common thing in this tables - id. It's same in both tables. How can i append column "tag" to first mysql database?
If you just want a view of the columns in the first table along with a possible matching tag, then use a query:
SELECT
t1.id, t1.name, t1.date, t2.tag
FROM table1 t1
LEFT JOIN table2 t2
ON t1.id = t2.id;
If you instead want to actually add a new tag column to the first table, then add that column and then do an update:
ALTER TABLE table1 ADD COLUMN tag VARCHAR(55);
UPDATE table1 t1
INNER JOIN table2 t2
ON t1.id = t2.id
SET t1.tag = t2.tag;
Im trying to create a view where specific Columns from 2 tables to be combined.
My Tables
Table1(Column1,Column2,Column3)
Table2(Column4,Column5,Column6)
Expected output
View1(Column2,Column3,Column6)
What query can i use to achieve that output?
CREATE VIEW [dbo].[View]
AS
SELECT a.Column2,a.Column3,b.Column6
FROM Table1 AS a
INNER JOIN Table2 AS b ON A.ID = B.ID -- your logic
If you have and id to join tables:
create view View1
select t1.column2, t1.column3, t2.column6
from table1 t1
join table2 t2 on t1.id = t2.id
If you don't have and id and want to get mixes columns without relationships:
create view View1
select t1.column2, t1.column3, t2.column6
from table1 t1
cross join table2 t2 on t1.id = t2.id
As Fourat and jarhl commented above, you need to explicitly define what columns are used to JOIN both tables. So using the below code change the ID column to the column that relates these two tables, and if it's more than one column the list them all separated by AND.
CREATE VIEW dbo.YourViewName
AS
SELECT t1.Column2, t1.Column3, t2.Column6
FROM Table1 t1
JOIN Table2 t2 ON t1.ID = t2.ID
GO
update table1 t1
inner join
table2 t2 on
t1.a = t2.a
set t1.b = t2.b,
t1.c = t2.c;
This code works to join 2 tables on column a. My problem is that I have about 500 columns which I want to update and am currently writing out each of the 500 columns in the code up to
t1.500 = t2.500;
This works, but it is slow and inefficient. Does anyone know how you can select * from table2 to update table1, keeping the join on t1.a = t2.a? All of the column names match exactly and am inserting all of the columns from table2. Was thinking of something like this below although I know that this is not correct. Thank you!
update table1 t1
inner join
table2 t2 on
t1.a = t2.a
set t1.* = t2.*;
I think there is no way to make update query with a wildcard in mysql. Don't know if it will properly fit to your problem, but you can try this workaround. :
DELETE FROM table2 WHERE id IN (<ids>);
Delete all the records from table2 that are satisfying given condition. And then insert the corresponding records from table1 to table2.
INSERT INTO table2
SELECT * FROM table1 WHERE id IN (<ids>);
Need help with a mysql query. I have 2 tables - table1 and table2. I am trying to update a field in table1 which is not included in table2 - named Status. And I want to update that field with the value of 'A'. table1 and table2 DO have a field in common - named Member_ID. Here is my query that is giving me errors:
UPDATE table1 SET Status='A' WHERE Member_ID=table2.Member_ID;
Is there some type of join that is needed? Any help would be appreciated. Thanks.
cdr6800
This can be done with exists
UPDATE table1 s
SET s.status = 'A'
WHERE EXISTS(select 1 from table2 t
WHERE t.member_id = s.member_id)
You have to JOIN table1 to table2 like this:
UPDATE table1 AS t1
INNER JOIN table2 AS t2 ON t1.Member_ID = t2.Member_ID
SET t1.Status='A'
This will update all table1 records having a Member_ID value that also exists in table2.
I have got two tables. I want to update MODEL in table2 when ITEM in table1 equals ITEM in table2.
Any Ideas?
In MySQL, you do it like this
UPDATE table1 t1
INNER JOIN table2 t2
ON t1.id = t2.id
SET t1.col1 = t2.col1,
t1.col2 = t2.col2
If I understand correctly, you just want to perform an UPDATE on table2 based on, presumably, foreign keys?
If that's right, this should work:
UPDATE
table2
JOIN table1
ON table1.ITEM = table2.ITEM
SET
MODEL = 'new value';
The table declaration in an UPDATE statement is the same as is specified in a SELECT statement - so you can use any type of JOIN that fits your table/data.
Docs for UPDATE, SELECT.
If you could add an actual query attempt, or something, that might be helpful. Can you try something like the following:
UPDATE table2 JOIN table1 ON table2.ITEM = table1.ITEM SET MODEL = ?