Access: how to write update from query? - ms-access

In my table Progetti I need to update the field Eroso with the query Query2.
The value to put on Progetti.Eroso has to be taken from the query [Fatture Query] where [Fatture Query].[Codice Progetto] = Progetti.[Codice Progetto].
The problem is that the SQL that I am using in Query2 could be wrong because I don't have the expected result: the system asks to insert [Fatture Query].Sum. The code in Query2 is the following.
UPDATE Progetti SET Progetti.Eroso = [Fatture Query].Sum;
EDIT
This is the SQL code of [Fatture Query]:
SELECT Fatture.[Codice Progetto], Sum(Fatture.Fattura) AS [Sum]
FROM Fatture
GROUP BY Fatture.[Codice Progetto];

Since your query Fatture Query uses an aggregate function (SUM) any query which references this query is no longer updateable, as there is an inherent one-to-many relationship as soon as the records are aggregated.
As such, I believe you will need to use a function such as DSum to calculate the appropriate value for each record to be updated, e.g.:
UPDATE
Progetti
SET
Progetti.Eroso = DSUM("Fattura", "Fatture", "[Codice Progetto] = '" & [Codice Progetto] & "'")
If instead the query from which you are sourcing the values had not used an aggregate function (e.g. SUM, MIN, MAX etc.) then I would have suggested incorporating an INNER JOIN in your UPDATE query such that the values used to update the target field are sourced from the appropriate records in your Fatture Query query, e.g.:
UPDATE
Progetti
INNER JOIN
[Fatture Query]
ON
Progetti.[Codice Progetto] = [Fatture Query].[Codice Progetto]
SET
Progetti.Eroso = [Fatture Query].Sum

I think the problem is that your update query doesn't specify how the rows in [Fatture Query] match the rows in [Progretti] so the system doesn't know which row to look at. Try using a WHERE clause like this:
UPDATE Progetti SET Progetti.Eroso = [Fatture Query].Sum WHERE [Fatture Query].[Codice Progetto] = Progetti.[Codice Progetto];

Related

UPDATE SELECT Access

This is an MS Access 2010 related question.
Is it possible to go the short route (A) and write an UPDATE statement using a SELECT statement in order to catch the relevant value or do I have to go the long route (B) and I will firstly need to query the data through a SELECT statement that I will save as a query and then refer to this saved query in my UPDATE statement?
Here is (A):
UPDATE tbl_A
SET tbl_A.Header1 = (SELECT F1 FROM tblStaging
WHERE tblStaging.F1 = 'ISSUER CODE')
WHERE (((tbl_A.TableName)='tblStaging'));
Here is B:
SELECT F1
FROM tblStaging
WHERE F1 = 'ISSUER CODE';
UPDATE tbl_A, Q_A_Sel_ISSUERCODE
SET tbl_A.Header1 = [Q_A_Sel_ISSUERCODE].[F1]
WHERE (((tbl_A.TableName)='tblStaging'));
Thank you
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Chris gave the solution:
UPDATE tbl_A, tblStaging
SET tbl_A.Header1 = tblStaging.F1
WHERE (((tblStaging.F1)='ISSUER CODE') AND ((tbl_A.TableName)='tblStaging'));
I'm not sure about Access, but in SQL Server you can use a single UPDATE statement like this (and I would've thought it should also work in Access):
UPDATE A
SET Header1 = S.F1
FROM tbl_A A, tblStaging S
WHERE S.F1 = 'ISSUER CODE' AND A.TableName ='tblStaging';
Although if that's exactly what you want to do, it's the same as:
UPDATE tbl_A SET Header1 = 'ISSUER CODE'
WHERE TableName = 'tblStaging';

Update using Select Statement

I wanna write a query like this :
UPDATE `test_credit`
SET `test_credit`.`credit`=(`test_credit`.`credit`-((`test_credit`.`credit`/100)*5))
WHERE `test_credit`.`name` = `users`.`uname`
in fact i want to get a query on users.uname = test_credit.name but mysql say it has error and realize users.uname as column
what is correct query ?
You need to explicitly join it with table users. As far from my understanding based on your query, you want to calculate the credit if the names exists on both tables.
Give this a try,
UPDATE test_credit a
INNER JOIN users b
ON a.name = b.uname
SET a.credit = (a.credit - ((a.credit/100) * 5.0))
-- WHERE b.parent= "example"

Update mysql cell after fetching related cell value via select?

SQL:
$mysqli->query("UPDATE results
SET result_value = '".$row[0]['logo_value']."'
WHERE logo_id = '".$mysqli->real_escape_string($_GET['logo_id'])."'
AND user_id = '".$user_data[0]['user_id']."'");
This results table also contains result_tries I'd like to fetch before doing update, so I can use it to modify result_value... Is there a way to do it in a single shot instead of first doing select and than doing update?
Is this possible?
Basically:
UPDATE results SET result_value = result_value + $row[0][logo_value]
for just a simple addition. You CAN use existing fields in the record being updated as part of the update, so if you don't want just addition, there's not too many limits on what logic you can use instead of just x = x + y.

Can't update a target table

I have two tables, one a options table (rot) that has the metrics of a set of rooms and another that has the current state of the rooms (rt). I want to have a daily event that updates the cost_to_date in the rt using the values stored in the rot.
When I tried the SQL:
UPDATE room_tbl SET COST_TO_DATE_rt = COST_TO_DATE_rt + (
SELECT PerDiem_rot FROM room_options_tbl, room_tbl
WHERE `ROOM_OPT_ID_rot` = `ROOM_OPT_ID_rt`
AND `ADULT_COUNT_rot` = `ADULT_COUNT_rt`)
I get the error: #1093 - You can't specify target table 'room_tbl' for update in FROM clause
My searching for a solution led me to try a temporary table using aliasing but my attempts at it have all resulted in syntax errors. Any help would be appreciated.
You can't use a query because its not supported by MySQL in an update clause
From 13.2.10. UPDATE Syntax
Currently, you cannot update a table and select from the same table in a subquery.
Instead try the following
UPDATE room_options_tbl, room_tbl
SET COST_TO_DATE_rt = COST_TO_DATE_rt + PerDiem_rot
WHERE `ROOM_OPT_ID_rot` = `ROOM_OPT_ID_rt`
AND `ADULT_COUNT_rot` = `ADULT_COUNT_rt`

MySQL Update a column depending on multi tables

I have a question regarding updating a MySQL database.
I have three tables: Match, Submission and SubmissionVersion. A SubmissionVersion can be set as 'Favorite'. But I can't just query UPDATE SubmissionVersion SET IsFavorite = 1 WHERE ID = $ID because of the relation to a Submission and than the Match. My question is how can I update a SubmissionVersion column with a MySQL Query with two joins? I've tried this query but I can't get it to work.
UPDATE
SubmissionVersion
JOIN
Submission
ON
Submission.ID, SubmissionVersion.SubmissionID
JOIN
Match
ON
Match.ID ON Submission.MatchID
SET
SubmissionVersion.IsFavorite = ".$Index."
WHERE
SubmissionVersion.ID = ".$ID."
AND
Match.ID = ".$MatchID
UPDATE SubmissionVersion sv
SET sv.IsFavorite = ".$Index."
WHERE sv.ID = ".$ID."
AND sv.ID IN (
SELECT s.ID
FROM Submission s
WHERE s.MatchID = ".$MatchID'")
If I understand your statement correctly, that should work.
I eliminated the Match table completely since you're just checking the value against a column in Submission.
Let's start with saying that MATCH is MySQL's reserved word, so needs to be put into backticks.
http://dev.mysql.com/doc/refman/5.1/en/reserved-words.html