I have two tables in access
table1 (name,nat) 16000 records
table2 (v_name,id) 189000 records
I want to update nat in table1 with id from table2. tried the following
UPDATE table1 SET nat = (SELECT table2.ID FROM table2 INNER JOIN table1 ON table2.V_name = table1.Name);
got the following
error: Operation must use an updateable query
You can try to use UPDATE .... JOIN in access
UPDATE table1 t1
INNER JOIN table2 t2
ON t2.V_name = t1.Name
SET t1.nat = t2.ID
My query is:
SELECT *
FROM table1
RIGHT JOIN table2 on table1.ID = any(table2.parents)
WHERE table1.ID = 1
My first table is:
ID | desc
123 | Data123
231 | Data231
My second table is:
ID | parents
1 | {123,231}
2 | {123}
But this query isn't working. I am trying to get the data from both 123 and 231, how do I fix this?
any(table1.parents)
parents doesn't exists in table1.
Try to change places
SELECT *
FROM table1
RIGHT JOIN table2 on table1.ID = any(table2.parents)
WHERE table2.ID = 1
You didn't specify what exactly you want, but if you just want to find rows in table1 where the IDs are present in table2.parents, the following should do it:
select t1.*
from table1 t1
where exists (select *
from table2 t2
where t1.id = any(t2.parents))
order by t1.id;
If you want information from table2 as well you need a join (and I don't think you want an outer join):
select *
from table1 t1
join table2 t2 on t1.id = any(t2.parents)
order by t1.id
Due to the nature of a join that would return the row (123,Data123) twice - don't know if you want that or not.
Example: http://rextester.com/PUOL76353
I've got two tables set up, here is a basic example of the setup:
Table1 Table2
____________ ____________
|id |date | |id |stuff |
|_____|______| |_____|______|
so they both have an id column. I'm trying to update table1 in this fashion:
update Table1
set [date] = (select [stuff]
from Table2
where Table2.id = id)
However, in the line where Table2.id = id, it uses the id field from Table2 instead of using the one from Table1.
When I try where Table2.id = Table1.id, I get an error. How can I keep tracking Table1's id per row to use in the subquery for Table2?
You need to reference the id inside the inner query to table 1.
This should solve your problem:
update Table1
set Table1.[date] = (select Table2.[stuff]
from Table2
where Table2.id = Table1.id)
Try this using Join:
Update t1 set t1.[date] = t2.[stuff]
from Table1 t1
join Table2 t2 on t1.id = t2.id
here's the join version,
update a
set a.[date] = b.[stuff]
FROM Table1 a
INNER JOIN Table2 b
ON a.ID = b.ID
Table 1:
id name desc
-----------------------
1 a abc
2 b def
3 c adf
Table 2:
id name desc
-----------------------
1 x 123
2 y 345
In oracle SQL, how do I run an sql update query that can update Table 1 with Table 2's name and desc using the same id? So the end result I would get is
Table 1:
id name desc
-----------------------
1 x 123
2 y 345
3 c adf
Question is taken from update one table with data from another, but specifically for oracle SQL.
This is called a correlated update
UPDATE table1 t1
SET (name, desc) = (SELECT t2.name, t2.desc
FROM table2 t2
WHERE t1.id = t2.id)
WHERE EXISTS (
SELECT 1
FROM table2 t2
WHERE t1.id = t2.id )
Assuming the join results in a key-preserved view, you could also
UPDATE (SELECT t1.id,
t1.name name1,
t1.desc desc1,
t2.name name2,
t2.desc desc2
FROM table1 t1,
table2 t2
WHERE t1.id = t2.id)
SET name1 = name2,
desc1 = desc2
Try this:
MERGE INTO table1 t1
USING
(
-- For more complicated queries you can use WITH clause here
SELECT * FROM table2
)t2
ON(t1.id = t2.id)
WHEN MATCHED THEN UPDATE SET
t1.name = t2.name,
t1.desc = t2.desc;
try
UPDATE Table1 T1 SET
T1.name = (SELECT T2.name FROM Table2 T2 WHERE T2.id = T1.id),
T1.desc = (SELECT T2.desc FROM Table2 T2 WHERE T2.id = T1.id)
WHERE T1.id IN (SELECT T2.id FROM Table2 T2 WHERE T2.id = T1.id);
Update table set column = (select...)
never worked for me since set only expects 1 value - SQL Error: ORA-01427: single-row subquery returns more than one row.
here's the solution:
BEGIN
For i in (select id, name, desc from table1)
LOOP
Update table2 set name = i.name, desc = i.desc where id = i.id;
END LOOP;
END;
That's how exactly you run it on SQLDeveloper worksheet. They say it's slow but that's the only solution that worked for me on this case.
Here seems to be an even better answer with 'in' clause that allows for multiple keys for the join:
update fp_active set STATE='E',
LAST_DATE_MAJ = sysdate where (client,code) in (select (client,code) from fp_detail
where valid = 1) ...
The full example is here:
http://forums.devshed.com/oracle-development-96/how-to-update-from-two-tables-195893.html - from web archive since link was dead.
The beef is in having the columns that you want to use as the key in parentheses in the where clause before 'in' and have the select statement with the same column names in parentheses.
where (column1,column2) in ( select (column1,column2) from table where "the set I want" );
BEGIN
For i in (select id, name, desc from table2)
LOOP
Update table1 set name = i.name, desc = i.desc where id = i.id and (name is null or desc is null);
END LOOP;
END;
If your table t1 and it's backup t2 have many columns, here's a compact way to do it.
In addition, my related problem was that only some of the columns were modified and many rows had no edits to these columns, so I wanted to leave those alone - basically restore a subset of columns from a backup of the entire table. If you want to just restore all rows, skip the where clause.
Of course the simpler way would be to delete and insert as select, but in my case I needed a solution with just updates.
The trick is that when you do select * from a pair of tables with duplicate column names, the 2nd one will get named _1. So here's what I came up with:
update (
select * from t1 join t2 on t2.id = t1.id
where id in (
select id from (
select id, col1, col2, ... from t2
minus select id, col1, col2, ... from t1
)
)
) set col1=col1_1, col2=col2_1, ...
I have a problem joining two tables:
table1
id name
1 aaa
2 bbb
3 ccc
table2
id table1_id name
1 1 x1
2 1 x2
3 2 s1
table1 is the main table, table2 contains attributes.
I need to join and search both tables, but display distinct results from first table.
When using JOIN I get multiple results from table2.
The scenario is I need to search main table TABLE1 and ALL ATTRIBUTES in TABLE2 and return if found
select distinct(name) from table1 inner join table2 on table1.id = table2.table1_id where table2.name = x2;
Should do the trick.
If you need entries which exists in both tables:
SELECT * from Table1 t1
WHERE YourConditionsHere
AND EXISTS (SELECT 1 from Table2 t2
WHERE t1.Id = t2.Table1_id
AND YourConditionsHere)
if you need entries from Table1 for which does not exists enteries in Table2
SELECT * from Table1 t1
LEFT JOIN
(SELECT * from Table2
WHERE YourConditionsHere
) t2
ON (t1.Id = t2.Table1_id)
WHERE YourConditionsHereForTable1
another option
select * from table1 t1 where t1.id in (select table1_id from table2 t2 where t2.name = "x1");
it's probably best to check query plains (i.e. EXPLAIN) for all suggested queries and check the one that performs best for your exact scenario.