Update a column of a table with data from another table if columns match PostgreSQL - sql

I want to add multiple values to a column if the value in the column matches another value in a column from another table.
e.g.
table1
column1.1
column1.2
table2
column2.1
column2.2
If column2.2 = column1.1 then update column1.2 with column 2.1
Should be able to write multiple values into column 2.1
Here is what I had but it's not working.
SET column1.2 = table2.column2.1
FROM table2
WHERE table1.column1.1 = table2.column2.2

You seem to want an update from another table. The syntax looks like this:
update table1
set column1 = table2.column1
from table2
where table1.column2 = table2.column2;
I can't follow the dance of column names in your question, but this is the structure of updating columns in one table from another table.

This should do it -
update table1
set column1.2 = table1.column1.2 || ' ' || table2.column2.1
from table2
where table1.column1.1 = table2.column2.2;

Related

UPDATE with join referencing same table - Redshift SQL

Assume I have a table named tab. Tab has a field called "version". Version is sequential.
In Redshift, I would like to update rows in Tab with the value from a previous row. Please tell me how to do this. Essentially:
UPDATE tab
SET tab.fieldA = tabPrior.fieldA
FROM tab tabPrior
WHERE tab.version = tabPrior.version + 1;
I would love to use a JOIN, but this does not seem to work in Redshift.
To Update with a JOIN statement you are better off writing it in a subquery.
UPDATE table
SET
col1 = TT.col1
col2 = TT.col2
FROM (SELECT T1.id, T1.col1, T2.col2
FROM T1
JOIN T2 ON T1. id ON T2. id) AS TT
WHERE table.id = TT.id
Also, I noticed that your syntax is the SET is not correct:
SET fieldA = tabPrior.fieldA
Redshift does not allow for you to pass the table when selecting the column since it goes under the assumption that you are updating 1 table at a time.
SET column = One or more columns that you want to modify. Columns that
aren't listed retain their current values. Do not include the table
name in the specification of a target column. For example, UPDATE tab
SET tab.col = 1 is invalid.
Link: https://docs.aws.amazon.com/redshift/latest/dg/r_UPDATE.html

updating sql query value with select statement

I am trying to execute a query which is something like:
update table set column=(select column1 from table1);
I just want to store the value from other table to my column
but when i try my sql query it says
ERROR 1242 (21000): Subquery returns more than 1 row
definitely this means my table1 contains more than 1 row so i want to know that is there any way to store data into column from other table with multiple row.
or basically saving content of other table as a text something like
update table set column='Data in text from other table';
You probably need a correlation clause:
update table
set column = (select column1 from table1 where table.col = table1.col);
You need to decide what column(s) are used for the correlation.
it will work as i am getting your requirement.Please let me know if your requirement is other i ll make changes in query
update table_name t1
inner join table1 t2 on t1.id =t2.id
set column =column1
Your nested query returns multiple rows so you encountered this error.
Try in this way
UPDATE FirstTable
SET FirstTable.ColumnName =tbl2.ColumnName
FROM SecondTable tbl2 WHERE tbl2.Id = FirstTable.Id
There should be a common id or something that will help to find exact row.

Update all rows for one column in a table with data in another table

I appreciate any advice on this..
I have two tables where I have to update a column in my primary table with data that resides in another secondary table. I cannot rely on views, etc as this data has to be able to be edited by the user in APEX in the future. I am basically pre-populating the data for the users to reduce their manual entry.
Primary Table = Table 1
Secondary Table = Table 2
Columns to be updated in Table 1 = FTE_ID, ACCOUNT_TYPE
Columns where the data will come from Table 2 = R_ID, ACCOUNT_TYPE
Common column in both tables = TABLE1.FID AND TABLE2.FID
Here is what I have tried, but I get "single-row subquery returns more than one row" because there are multiple table1.fid rows in table1. I basically want to perform this update for ALL rows where TABLE1.FID = TABLE2.FID.
Here is my attempt:
UPDATE TABLE1
SET TABLE1.FTE_ID =
(SELECT TABLE2.R_ID FROM TABLE2 WHERE TABLE1.FID = TABLE2.FID);
Error:
single-row subquery returns more than one row
Thanks for your help,
You can fix the proximate problem by using aggregation or row number:
UPDATE TABLE1
SET TABLE1.FTE_ID = (SELECT MAX(TABLE2.R_ID)
FROM TABLE2
WHERE TABLE1.FID = TABLE2.FID
);
The subquery can only return one row; it is an "arbitrary" value from the possible matching values.
If the field is a character field and you want all matching values, then perhaps listagg is more appropriate:
UPDATE TABLE1
SET TABLE1.FTE_ID = (SELECT LISTAGG(t2.R_ID, ',') WITHIN GROUP (ORDER BY t2.R_ID)
FROM TABLE2 t2
WHERE TABLE1.FID = t2.FID
);

How to copy column values from one database to empty column in other database?

I have two databases.
Alarm
TMP
I have a table in Alarm, where in a table there is one empty column with null values.
And I have a single column table in TMP.
I want to copy this single column values to my table in Alarm database.
What I tried so far is,
update [Alarm].[dbo].[AlarmDetails] set Alarm_Message = (select * from [TMP].[dbo].[AlarmDetails$])
where 1=1
The error is
Subquery returned more than 1 value.
Please note this,
NOTE: There is no id column in source table. Only one table & one column, Alarm Message.
I know the cause of error, but how should I modify my SQL.
Thank You.
Here's an example of copying a column:
update dst
set Alarm_Message = src.AlarmMessage
from Alarm.dbo.AlarmDetails dst
join TMP.dbo.AlarmDetails src
on dst.id = src.id
You did not specify how the tables are related, so I assumed they both have an id column.
You need something like this.
update t1
set
t1.<something1> = t2.<something2>
from
[Alarm].[dbo].[AlarmDetails] t1
join [TMP].[dbo].[AlarmDetails] t2 on (t1.<cols1> = t2.<cols2>)
UPDATE results SET results.platform_to_insert = (
SELECT correct_platform
FROM build
WHERE results.BuildID=build.BuildID LIMIT 1
);

How do I Update a column that is like %another column% in SQL

I would like to run an Update on a table based upon values in another table.
So I have Table 1 with Column A
And I have Table 2 with Column B
I want to run an update so that every row in Column A gets updated with 'RANDOM STRING' if Column A is LIKE Column B.
Pretty simple up to here. However the string in Column B could occur anywhere in the String in Column A.
So the query should run something like this
UPDATE Table1
SET ColumnA = 'RANDOM STRING'
WHERE ColumnA LIKE '%Table2.ColumnB%'
However no rows get updated when I use this, though the WHERE condition should definitely return results
I am running SQL server 2008
try this
update table1
set ColumnA = 'RANDOM STRING'
where ColumnA in
(select table1.ColumnA from table1 inner join table2 on table1.ColumnA like '%'+Table2.ColumnB+'%')
In MySql,
You can do this.
UPDATE Table1
SET ColumnA = 'RANDOM STRING' from table1 , table2
WHERE table1.ColumnA LIKE concat('%',table2.columnb,'%')