I have been trying to move some data which relates to a specific column from one table to another. They both have a matching objectID.
So what I am trying to do is:
TABLE 1
ObjectID
Field with Data
TABLE 2
ObjectID
FIELD with NEW column
So the object ID relate to each other. All it is I am trying to do is move the data from Table 1 to Table 2 with the new column.
I have tried following but cant seem to get it all working. Is there anything that can be suggested that may help or point in me the right direction.
update Table2 a
Set a.NewColumn = (Select *
From Table1 b WHERE a.OBJECTID = b.OBJECTID
)
Hm, maybe I don't get it but why don't you use a INSERT?
INSERT INTO TableB(...columns...)
SELECT ...columns...
FROM TableA
You can use join update syntax for this, need to make sure that the Table2 already has data and you are updating a new column in there from Table1
update Table2 t2
join Table1 t1 on t1.OBJECTID = t2.OBJECTID
set t2.NewColumn = t1.Field
You can write query like this.
INSERT INTO Table2(ObjectID,Field)
SELECT ObjectID,Field
FROM Table1.
And you can put any default value in extra column.
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.
In SQL (whichever variant you like, say MySQL or MonetDB) it's very intuitive and straightforward to do:
CREATE TABLE t2 AS SELECT c1 FROM t1;
and get the selection result as a new column in a new table. But what if you want the result as a new column in the same table (table t1)? Let's assume c1 is of type INT and may be null. We would need to write:
ALTER TABLE t1 ADD c2 INT;
UPDATE TABLE t1 SET c2 = c1;
and that's the easy version, since if it's a non-null column we would need to initialize the new column with some value; and if the data comes from multiple tables I'd need some kind of inner query in the UPDATE (if it would be at all possible).
Can I select-into-a-column with a single command somehow?
You use join:
UPDATE t1 JOIN
t2
ON t2.? = t1.?
SET t1.c2 = t2.c1;
The ? is a placeholder for the column used to identity which row in t2 should update which row in t1.
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 am new to MySQL and learning it to my own. Actually I want to copy a column from a table into my existing table column! suppose that my existing table is:
where pid values are inserted by default!
now i want to copy a column from another table using:
INSERT INTO exist_tab(FirstLevel) SELECT some_col FROM another_table;
so that the values should come inside FirstLevel Column.
but the problem is that the copies values come below the pid values in FirstLevel Column as:
see that the firstlevel comes below! what is wrong with it? I need the "H" value against 19 but i dont want to use wild cards just want to copy the new data against old column data
thanks
I am new to this kind a work please can somebody give me any idea how to do it please!
thanks in advance
INSERT and UPDATE is different Command to Perform Different Task.
INSERT :Insert New Record into the table
Update:Update Existing Record in table If Exist.
NOT SURE ABOUT IT:(i'm Not Familiar With MYSQL)
Update a set
a.FirstLevel=b.some_col
from
exist_tab a join another_table b on a.Id=b.Id
Or You can Try :
update exist_tab a set a.FirstLevel=
(select top 1 some_col from another_table where Id=a.Id)
EDIT2:
update exist_tab a set a.FirstLevel=
(select top 1 some_col from another_table)
You Can Find Here.
You are using INSERT statement here. INSERT will create a new record in the table. You have to use UPDATE for updating a particular column in the existing table like this:
UPDATE exist_tab
SET FirstLevel = (SELECT some_col FROM another_table)
If you want any conditional update then you can use JOIN like this:
UPDATE exist_tab a
LEFT JOIN another_table b ON
a.pid = b.id
SET FirstLevel = a.some_col;
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.