Updating MS Access table with a specific value based on another table - ms-access

I'm trying to update a yes/no column in a table based on a lack of matches from a second table as follows:
UPDATE Table1
INNER JOIN Table2
ON (Table1.Date_last_action = Table2.MaxOfDate_last_action)
AND (Table1.Office = Table2.Office)
AND (Table1.Response_ID = Table2.Response_ID)
SET Table1.IsDeprectated = 1
WHERE (([Table2].[MaxOfDate_last_action] Is Null));
OR
UPDATE Table1
INNER JOIN Table2
ON (Table1.Date_last_action <> Table2.MaxOfDate_last_action)
AND (Table1.Office = Table2.Office)
AND (Table1.Response_ID = Table2.Response_ID)
SET Table1.IsDeprectated = 1;
Neither of which is working.
What I'm aiming for is that when the update query is executed that the IsDeprecated column is set to True for all rows in Table1 that DO NOT have a matching row in Table2
Is this possible?
[Edit following Tim's comment]

You could use exists logic along with a subquery:
UPDATE Table1 t1
SET t1.IsDeprectated = 1
WHERE NOT EXISTS (SELECT 1 FROM Table2 t2
WHERE t1.Date_last_action <> t2.MaxOfDate_last_action AND
t1.Office = t2.Office AND
t1.Response_ID = t2.Response_ID);

Related

Using group by in SET clause

I'm trying to update a column of a table so that is equal to the count of something in another table. Like this:
UPDATE TABLE
SET TOTAL = (SELECT COUNT(f1)
FROM TABLE2
GROUP BY f2);
But I keep getting sub query returns more than 1 row, and I can't think of how to fix it.
UPDATE (copied from the comment)
f2 is the relation between TABLE and TABLE2 – Thomasd d
Based on your comment
f2 is the relation between TABLE and TABLE2
you probably want something like this
UPDATE TABLE T1, (SELECT f2, COUNT(F1) cnt FROM TABLE2 GROUP BY f2) T2
SET T1.TOTAL = T2.cnt
WHERE T1.f2=T2.f2
adapt T1.f2 if necessary
UPDATE t1
SET total = ( SELECT COUNT(f1)
FROM t2
WHERE t1.f2 = t2.f2 );
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=91de17deff657f66fa54b42fe20ed3c5
Add WHERE total IS NULL if you do not need to recalculate values for rows which have a value already.
Your subquery is returning multiple values and your SET statement is only expecting one. This might fix your code if that is what you are looking for.
UPDATE TABLE
SET TOTAL = (SELECT COUNT(f1)
FROM TABLE2)

How do I update values in a table from another table with the exact same fields?

Let's say I have a table called AA.
I distributed 2 copies for an update.
In 1 copy, called Table1, all values in Field2 are updated updated where the value in Field1 is AA.
In the other copy Table2 all values are updated where the value in Field1 is BB
My question is:
How do I update table AA with this new information?
This query will update AA.Field2 with values from Table1.Field2 if ID match and where Field1=AA``
UPDATE AA INNER JOIN Table1 ON (AA.Field1 = Table1.Field1) AND (AA.Id = Table1.Id) SET AA.Field2 = [Table1].[Field2]
WHERE (((Table1.Field1)='AA'));
You can make a second query but with parameters of TableB.
UPDATE AA INNER JOIN Table2 ON (AA.Field1 = Table2.Field1) AND (AA.Id = Table2.Id) SET AA.Field2 = [Table2].[Field2]
WHERE (((Table2.Field1)="AA"));
UPDATE: You can make all updates on a single Query:
UPDATE Table1 INNER JOIN (Table2 INNER JOIN AA ON (Table2.Field1 = AA.Field1) AND (Table2.Id = AA.Id)) ON (Table1.Field1 = AA.Field1) AND (Table1.Id = AA.Id) SET AA.Field2 = IIf([AA]![Field1]="AA",[Table1]![Field2],[Table2]![Field2]);

Use SELECT result to query another table's row

I have a pretty simple MySQL query to implement, but I can't figure out how...
I have two tables, T1 and T2.
What I need to do:
From T1, I retrieve an ID based on a CODE value:
SELECT id FROM T1 WHERE code = '$code';
Then I need to use this ID (so the value I just retrieved) to update a specific row in T2 (the name of the row will match the ID's value).
I was thinking about using either subqueries or user-defined variables, but no matter how I try it I can't get it done.
If you have any code snippet that can help me doing that, I would appreciate it as well!
EDIT
Just to clarify something: I don't know the name of the column that I need to update in T2, since that name will be the value I retrieve from T1.
So for example, if the ID I get from T1 is "03", it will update the column named "03" in T2.
EDIT 2
Here's a little schema of what I intend to achieve (hoping I make myself clearer, I'm sorry for the misunderstanding...)
UPDATE T2 SET COL = YOUR_VALUE
WHERE T2.ID = (SELECT id FROM T1 WHERE code = '$code')
UPDATE: If the sub query returns more than one row then you can use from IN operator
UPDATE T2 SET COL = YOUR_VALUE
WHERE T2.ID IN (SELECT id FROM T1 WHERE code = '$code')
Use an UPDATE with a JOIN:
UPDATE T2
CROSS JOIN T1
SET T2.`0` = IF(T1.id = 0, T1.someColumn, T2.`0`),
T2.`1` = IF(T1.id = 1, T1.someColumn, T2.`1`),
T2.`2` = IF(T1.id = 1, T1.someColumn, T2.`1`)
WHERE T1.code = '$code'
Replace someColumn with the column in T1 containing the value you want to put into T2.
you can update without using the subquery just using join
update t2
inner join t1 on t2.name = t1.id and t1.code ='$code'
set t2.my_col = 'my_value'
but you should not use var in your query you are at risk for sql injection take a look at you mysql driver for param_binding
UPDATE T2 SET COL = YOUR_VALUE WHERE EXISTS
(SELECT 1 FROM T1 WHERE T2.id=T2.id AND code = '$code')
You can update T2 with the next SQL if you expect at least one row from the inner query
UPDATE T2 SET COLUMN = VALUE
WHERE T2.ID IN (SELECT ID FROM T1 WHERE CODE = $code)
Otherwise, if from T1 you are sure you will get only 1 record
UPDATE T2 SET COLUMN = VALUE
WHERE T2.ID = (SELECT ID FROM T1 WHERE CODE = $code)

updating a column in table 1 by joining information from table 2

I have tabel 1 that has VIN which i want to update.
table 1 and table 2 has OBJ ID and POID (respectively) which are same.
I only know device ID which is present in table 2.
update table.1 set VIN = '5TDKK3DC6BS018229'
from table 2, table 1
where 2.device ID = 'TCAXLcKkt3'
and 2.OBJ = 1.POID;
I am getting SQL command not properly ended.
Make sure you remove the semi-colon at end of the query since some databases will complain if its there in TOAD.
If you are using SQL Server then following query will work.
UPDATE table1
SET VIN = '5TDKK3DC6BS018229'
FROM table1
INNER JOIN table2 ON table1.POID = table2.OBJ
WHERE table2.deviceID = 'TCAXLcKkt3'
If you want the same query for MySQL, then use the one below.
UPDATE table1 a
JOIN table2 b
ON a.poid = b.obj
SET a.vin = '5TDKK3DC6BS018229'
WHERE b.deviceid = 'TCAXLcKkt3'
If you are using Oracle, then use the query as below.
UPDATE table1
SET table1.vin = '5TDKK3DC6BS018229'
where exists (SELECT table2.obj
FROM table2
WHERE table2.obj = table1.poid
AND table2.deviceid = 'TCAXLcKkt3')
Error message looks like you are using Oracle.
If am not wrong this is what you are looking for
UPDATE table1
SET vin = '5TDKK3DC6BS018229'
WHERE EXISTS (SELECT 1
FROM table2 B
WHERE table1.obj = B.poid
AND B."device id" = 'TCAXLcKkt3')
In Oracle to update table from another table using Join try the below syntax
UPDATE
(SELECT A.VIN
FROM table1 A
INNER JOIN table2 B
ON A.OBJ = B.POID
WHERE B."device ID" = 'TCAXLcKkt3'
) t
SET T.VIN = '5TDKK3DC6BS018229'

How to update based on anoter row in the same table?

I want to create a query that updates an int based on the int of the row with an id that is 1 higher.
I have tried this query, but it says that i can't label the table in an update statement. But how do i reference it in my subquery?
update t1 a set `int1` = (select `int1` from t1 b where b.id=a.id+1);
How can I overcome that I can't use an alias?
Try this one -
UPDATE
t1 a
JOIN t1 b
ON b.id = a.id + 1
SET
a.int1 = b.int1;
If there are holes in id values, the query may be changed.