Losing my table inside of a subquery... what's the issue? - sql

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

Related

Update table column based on another table based on ID

I have 2 tables 'table1' and 'table2'.
table1 has 10,000 records and table2 has 5,000 records.
Both tables have "RECORD_ID" column. All RECORD_ID's that are in table2 can be found in table1.
I want to UPDATE the "PRICE" column of table1 based on the "RECORD_ID" column of table2.
update table1 set PRICE = table2.PRICE where RECORD_ID = table2.RECORD_ID
I got this error message:
SQL0206N "table2.PRICE" is not valid in the context where it is used
SQLSTATE=42703
I am using DB2.
UPDATE table1 SET table1.price = (SELECT table2.price FROM table2 WHERE table2.record_id = table1.record_id)
Try this:
UPDATE table1 f1
SET f1.price =(
SELECT f2.price
FROM table2 f2
WHERE f2.record_id = f1.record_id
)
WHERE exists
(
SELECT f2.price
FROM table2 f2
WHERE f2.record_id = f1.record_id
)
You have to use a join like this:
UPDATE
Table1
SET
Table1.Column = T2.Column
FROM
Table1 T1
INNER JOIN
Table2 T2
ON
T1.PK = T2.FK;

Trying to get value from other table using any in SQL

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

Update multiple rows using select statement

I have these tables and values:
Table1
------------------------
ID | Value
------------------------
2 | asdf
4 | fdsa
5 | aaaa
Table2
------------------------
ID | Value
------------------------
2 | bbbb
4 | bbbb
5 | bbbb
I want to update all the values in Table2 using the values in Table1 with their respective ID's.
Is there a way to do that with a simple SQL query?
Run a select to make sure it is what you want
SELECT t1.value AS NEWVALUEFROMTABLE1,t2.value AS OLDVALUETABLE2,*
FROM Table2 t2
INNER JOIN Table1 t1 on t1.ID = t2.ID
Update
UPDATE Table2
SET Value = t1.Value
FROM Table2 t2
INNER JOIN Table1 t1 on t1.ID = t2.ID
Also, consider using BEGIN TRAN so you can roll it back if needed, but make sure you COMMIT it when you are satisfied.
If you have ids in both tables, the following works:
update table2
set value = (select value from table1 where table1.id = table2.id)
Perhaps a better approach is a join:
update table2
set value = table1.value
from table1
where table1.id = table2.id
Note that this syntax works in SQL Server but may be different in other databases.
You can use alias to improve the query:
UPDATE t1
SET t1.Value = t2.Value
FROM table1 AS t1
INNER JOIN
table2 AS t2
ON t1.ID = t2.ID
None of above answers worked for me in MySQL, the following query worked though:
UPDATE
Table1 t1
JOIN
Table2 t2 ON t1.ID=t2.ID
SET
t1.value =t2.value
WHERE
...
I have used this one on MySQL, MS Access and SQL Server. The id fields are the fields on wich the tables coincide, not necesarily the primary index.
UPDATE DestTable INNER JOIN SourceTable ON DestTable.idField = SourceTable.idField SET DestTable.Field1 = SourceTable.Field1, DestTable.Field2 = SourceTable.Field2...

Column not found error with simple SQL query

I know its almost there, but base is telling me it can't find a column called table1.id when I know its there!
UPDATE table2 SET col1 = (SELECT field1 FROM table1 WHERE table2.id = table1.id) WHERE table1.id = table2.id
UPDATE table2 SET col1 = (SELECT field1 FROM table1 WHERE table2.id = table1.id)
table1 is unknown in the outer SQL.
What I get from your query, this will work
UPDATE table2 SET col1 = t1.field1
FROM table2 t2 INNER JOIN table1 t1 ON t2.id = t1.id
Instead of using a WHERE clause, try using an INNER JOIN clause. It is indeed late so forgive me for my code haha
UPDATE table2
SET col1 = (SELECT field1
FROM table1
WHERE table2.id = table1.id)
INNER JOIN table1
ON table2.id = table1.id
Option 1: No need to have outer WHERE clause.
Option 2: Do not use inner query unneccesarily. Use Inner Join instead

T-SQL for merging data from one table to another

Let's say there are 2 tables Table1 { ID, Name, Other } and Table2 { ID, Name, Other }. Both of them have identical records with the same IDs except that in Table1 all Name values are NULL. How can I import Name values from Table2 to Table1 using T-SQL (SQL Server 2008)?
Update Table1
Set Table1.Name = Table2.Name
From
Table1 INNER JOIN Table2 on Table1.ID = Table2.ID
You're looking for the MERGE command, which is like the UPSERT that you've probably read about elsewhere. Here's a quick article about it.
UPDATE Table1
SET Table1.Name = Table2.Name
FROM Table2
WHERE Table1.Id = Table2.Id
--AND Table1.Name IS NULL
Just join the tables and update:
update t1
set [Name] = t2.Name
from Table1 t1
inner join Table2 t2 on t2.ID = t1.ID