Update one column where another column within same table equals a column in another table - sql

I have a Query for retrieving data from one table where one column equals one specific value and one column equals a column in another table.
Like this:
SELECT a.IdOrdre, b.FR_Ordre
FROM Ordre a
INNER JOIN OrdreKategori b ON a.IdOrdre=b.FR_Ordre
WHERE b.FR_Kategori=57
I now would like to update the column FR_Kategori to another value than 57. How would I structure the UPDATE Query in order to achieve this?

Since you didn't tag your DBMS, and every one has different syntax for UPDATE..JOIN , here is an ANSI-SQL answer:
UPDATE OrdreKategori t
SET t.fr_kategori = Other_Val
WHERE EXISTS(SELECT 1 FROM Ordre s
where s.IdOrdre=t.FR_Ordre)
AND t.fr_kategori = 57

Related

How do I update every row of a particular column with values from another column?

I am trying to update a column called software_id with a possible three values from a lookup table. So update user softwares software id column with the values from the softwares table of the id column. But there are only three rows in the softwares table. In the user softwares table there are over 1000. But when I run the query below only three rows get updated and the rest are left as null.
So the softwares id column is number and goes 1,2,3 and the user_sofwares software_id column is all null.
When I run the following query.
UPDATE user_software c
SET sotware_id = (
SELECT DISTINCT id
FROM softwares
WHERE id = c.id
);
Only the first three rows of the destination table get updated. I want every row on the user_softwares column to be updated with either 1,2 or 3. So it should be 1,2,3,1,2,3 for example.
I keep getting this error whenever i tried to update all rows
Error report - ORA-01427: single-row subquery returns more than one row
You can do this with a MERGE statement:
MERGE INTO (SELECT software_id, 1, ora_hash(ROWID, 2) + 1 AS fake_id
FROM user_software) u_soft
USING (SELECT DISTINCT id
FROM softwares) sftw
ON (sftw.id = u_soft.fake_id)
WHEN MATCHED THEN UPDATE SET u_soft.software_id = sftw.id;
(considering your matches are unique)

SQL update tableA.column where the column and value is in a separate table

I have tableA with the columns ID, ColumnHeader, Value.
I'm trying to update tableB where ID, and the value in tableB.ColumnHeader with tableB.Value.
Essentially, the column headers for tableB are in a column in Table A and not column headers themselves.
I'm stuck on specifying the table name. For example, how do I run this query when I only have tablename.____ where the blank is in a column in a separate table?
update tableB set table.____ .....
As seen in the screenshot below, in Table B, 4 should become 1, and 8 should become 2, and 12 should become 3. Thanks so much.
this is a example from Northwind database:
UPDATE dbo.Products
SET dbo.Products.CategoryID = c.CategoryID
FROM Products
INNER JOIN dbo.Categories c ON dbo.Products.CategoryID = c.CategoryID
I am not getting which table and column you want to update with which table and column.
also relationship is not clear.
This doesn't make sense to me. Are you trying to update table B or A? Are there like values in table B and A that you want updated?
The basic code is as shown below:
update tableb
set column id = 'value you're updating'
where column id = 'value you're looking for' --always make this unique like a key etc.

Update column in table with value from another

I need to update one column called Ident in table a to be a value from table b, as shown below. Both the change and keep values in table b are valid Idents which will appear in table a. I need to find the Change value in table b in table a, and then change the value in table a to the Keep value in table b.
Statement i'm using is:
update p set p.ident=t.keep
from pclscvq p inner join #tmp t on (p.ident=t.change)
where t.change=p.ident
Table B:
Change Keep
0004P 0004R
0004X 0004Y
00055 00056
00057 00058
0005B 0005C
0005K 0005L
0005Z 00060
00065 00066
0006X 0006Y
00070 00071
Very much stuck.
Using Advantage SQL

Insert columnA values of Table 1 into another table if match occur

I have two tables.Table A has 4 columns. And table B has two columns.I want to insert value of one column of tabel A from one column of table B based on condtion if id matches.
how i can do this ? For example if [Movieid] in 1st table =[IMDBid] in second table then insert [count] of table 1=[CB] in table 2.
i want to do it once for full table.
[column] -> these are colums
i m using sql server.
Tabel 1 : Movieid,count,
Tabel 2: IMDBid, CB
Results which i want: i want to insert values of CB column in count where Movieid=IMDBid
Tabel 1 :
(Nick Id,MovieId,Rating,MovId)-> (1,4972,6.25,?)(1,24216,7.25,?)
Tabel 2 :
(Imdbid,Title,ImdbPyId,Id)-> (4972,hello,32450,1)(24216,hi,62450,2)
Insert /fill value of MovId(tabel1) using values Id(tabel2)where MovieId(tabel1)==Imdbid(tabel2)
You can do it like,
UPDATE tabl1
SET count = (SELECT CB FROM tabl2 WHERE IMDBid = Movieid)
But it is gonna blow up if you have multiple values returning from the subquery.
So make sure to use the appropriate function to get the single value from that subquery whichever meets your requirements.
If your tables have 1:1 relationship then it should be fine. But if it is 1:n then you need to use either aggragate functions or the TOP 1 clause in that subqyery.
solution is UPDATE tabl1
SET count = (SELECT CB FROM tabl2 WHERE IMDBid = Movieid)

Access combo box to return value from another table

I have a table called initial_eval that has a field called Partno.
What I would like to do is match the Partno value in a field
also called 'Partno' from a table called 'update'.
What I would then like to do is return the value in the
'consumption' field of 'update' where the 'Partno' values match.
I'm ok with SQL and join stuff, but I can't crack this one
Please help.
SELECT U.CONSUMPTION FROM UPDATE U
WHERE U.PARTNO IN (SELECT PARTNO FROM INITIAL_EVAL)
This assumes there is no condition while fetching from INITIAL_EVAL
SELECT U.CONSUMPTION FROM UPDATE U
WHERE U.PARTNO = (SELECT PARTNO FROM INITIAL_EVAL WHERE **some condition**)
In this case you need to frame the inner query in a such a way that it returns exactly one row which will automatically map to your CONSUMPTION table row.