UPDATE query with inner joined query - sql

I haved saved SELECT query. I need create update query to update table field with value from saved select query.
Im getting error "Operation must use an updatable query".
Problem is that saved select query result not contain primary key.
UPDATE [table] INNER JOIN
[saved_select_query]
ON [table].id_field = [saved_select_query].[my_field]
SET [table].[target_field] = [saved_select_query]![source_field]);
Im also try with select subquery instead of inner join, but same error.

Perhaps a DLookUp() will do the trick:
UPDATE [table] SET
[target_field] = DLookUp("source_field", "saved_select_query", "my_field=" & id_field)
... or, if the joined field is text ...
UPDATE [table] SET
[target_field] = DLookUp("source_field", "saved_select_query", "my_field='" & id_field & "'")

I'm not sure I completely understand what you are asking.
If you are asking what syntax to use when performing an update with an inner join.
UPDATE tableAlias
SET Column = Value
FROM table1 as tableAlias
INNER JOIN table2 as table2Alias on tableAlias.key = table2Alias.key

your query is incorrect , try this.
UPDATE [table]
SET [table].[target_field] = [table2].
[source_field])
from (select *from
[table] INNER JOIN
[saved_select_query] a
ON [table].id_field =
a.[my_field] )
table2

I got it to work using the following:
UPDATE [table]
SET [table].[target_Field] = (SELECT [source_field] FROM [Saved_Select_Query]
WHERE [table].[id_field] = [Saved_Select_Query].[my_field])
You can't use an JOIN on an UPDATE statement directly, so you need to join the tables in a subquery.

Related

Update Row, Inner Join With Getting Top Row

Using MariaDB
I am trying to update the membershipRenewDate(In officiantsDetails Table) using the MAX value from renewDate(in officiantsRenewals Table)
Inner Join on officiant_id (same column name for both tables)
I have something like this but am getting syntax error.
UPDATE officiantsDetails offd
SET offd.membershipRenewDate = offr.renewDate
FROM (SELECT TOP (1) renewDate, officiant_id FROM officiantsRenewals ORDER BY renewDate DESC ) as offr
WHERE offd.officiant_id = offr.officiant_id
You should be able to use a Common Table Expression (CTE) for this. You use the WITH clause to define your "subquery", and then INNER JOIN the CTE to the table you want to update. It would look something like this:
WITH Top1RenewDate AS
(
SELECT TOP (1)
renewDate,
officiant_id
FROM officiantsRenewals
ORDER BY renewDate DESC
)
UPDATE offd
SET offd.membershipRenewDate = offr.renewDate
FROM officiantsDetails offd
INNER JOIN Top1RenewDate offr ON offd.officiants_id = offr.officiants_id
This same syntax will also work for SQL Server, although you'd need to add a ; before the WITH keyword on that system.
The syntax you are using is not MariaDB. In MariaDB, you can use a JOIN:
UPDATE officiantsDetails offd JOIN
(SELECT renewDate, officiant_id
FROM officiantsRenewals
ORDER BY renewDate DESC
LIMIT 1
) offr
ON offd.officiant_id = offr.officiant_id
SET offd.membershipRenewDate = offr.renewDate;
The syntax you are using is more reminiscent of SQL Server.

Access - query update with Inner Join

I have this query:
UPDATE client
SET client.[client_history] = 10
FROM [T_CLIENT] AS client
INNER JOIN (SELECT [client_id], SUM([final_price])
FROM [T_PURCHASE]
GROUP BY [client_id]) AS p
ON client.[client_id] = p.[client_id]
When i execute this query on access, i get "Syntax Error".
Did you see something wrong?
Thank you
You can use a DSUM to sum from a different table in an update query. Subqueries with aggregates won't work, because they're not updateable.
UPDATE t_client
SET [client_history] = DSUM("final_price", "T_PURCHASE", "client_id = " & client_id)
Does the syntax work without FROM:
UPDATE [T_CLIENT] AS client INNER JOIN
(SELECT [client_id], SUM([final_price])
FROM [T_PURCHASE]
GROUP BY [client_id]
) AS p
ON client.[client_id] = p.[client_id]
SET client.[client_history] = 10;

SQL updating master table with updates from update table

I am trying to update my master table from my updates table, What it wrong with the below query.
UPDATE master_table
SET master_table.description = master_table_import.description
FROM master_table_import
WHERE master_table.user_id = master_table_import.user_id
You are missing a join between the tables. Try something like this:
UPDATE mt SET mt.Description = mti.Description
FROM master_table mt
INNER JOIN master_table_import mti
ON mt.user_id = mti.user_id;
It is always good idea to use aliases for tables. To update you have join your target table with source table:
UPDATE mt
SET description = mti.description
FROM master_table mt INNER JOIN master_table_import mti
WHERE mt.user_id = mti.user_id

Sqlite Update query using a subquery

I have to update table test_test column "testconsent_id" with the id value of table test_groupedconsent, where the patient_id in test_test and patient_id in test_groupedconsent table match and
also creation_date in both table match.
I'm using the below query but getting error -- "near "as": syntax error".
what is wrong with the query?
Update test_test as Tinner join (select id,patient_id,creation_date from test_groupedconsent) as Aon A.patient_id = T.patient_id and A.creation_date = T.creation_dateset T.testconsent_id = A.id;
You cannot use a join directly in an UPDATE statement.
You have to use a correlated subquery to look up the desired value (in the subquery, you can do whatever you want, but in this case, you don't even need a join):
UPDATE test_test
SET testconsent_id = (SELECT id
FROM test_groupedconsent
WHERE patient_id = test_test.patient_id
AND creation_date = test_test.creation_date);
sounds like it took the 'as' after join for the joined tables so either put as in the (... as ...) or bring "ON COMMAND" before as!
like this -> (table1 join table2 on table1.fiel = table2.field) as something
UPDATE TABLE test_test
SET testconsent_id =
(SELECT testconsent_id FROM test_groupedconsent AS A, test_test AS B
WHERE A.patient_id = B.patient_id AND A.creation_date = B.A.creation_date)

Alternative for exists query

I have the following query :
update tab1.abc,
tab1.sbd = (select tab2.abc, tab2.sbd from tab2,tab1 where --some conditions)
where exists(select tab2.abc, tab2.sbd from tab2,tab1 where --some conditions)
Now my task is to optmize this query.
I believe removing the exists clause or combining both the where clauses would help a lot.
But how to do it ?
PS : exists clause is in place because I want the number of rows updated to be zero if the select clause returns zero rows.
JOIN the two tables instead of EXISTS. Something like the following:
UPDATE tab1
INNER JOIN tab2 ON --some join condition
SET sbd = --something
AND abc = --other something
WHERE --some conditions
you can use IN keyword
update tab1.abc,
tab1.sbd = (select tab2.abc, tab2.sbd from tab2,tab1 where --some conditions)
where something in
(select tab2.abc, tab2.sbd from tab2,tab1 where --some conditions)
refer below links:
IN (TSql)
SQL IN