Microsoft Access Update Statement with Inner Joins and Subqueries - ms-access

I am having a tough time figuring out how to do this update query. Basically I need to update a table named tblOpenJobs. It needs to be updated with the dbo_WorkOrders table with the Max Install date. But there is not direct relationship between those two tables you need to have the dbo_premise table between. Here is my query, what am I doing wrong?
UPDATE tblOpenJobs
INNER JOIN (dbo_Premise INNER JOIN dbo_WorkOrders w (WHERE w.InstallDate IN
(SELECT MAX(InstallDate) FROM dbo_WorkOrders WHERE dbo_WorkOrders.PremiseKey = w.PremiseKey))
ON (dbo_Premise.PremiseKey = w.PremiseKey)
ON tblOpenJobs.ServiceOrderNum = dbo_Premise.AccountNumber
SET tblOpenJobs.InstallerID = w.InstallerID,
tblOpenJobs.InstallDate= w.InstallDate,
tblOpenJobs.New_Serial_num= w.NewSerial,
tblOpenJobs.Old_Reading= w.OldRead;

I checked this in Access 2007 query window:
Your query seems neither Transact-SQL, neither Access, as the two have different syntax.
In Access, table aliasing must use the keyword AS, while Transact-SQL does not require:
UPDATE ((tblOpenJobs
INNER JOIN dbo_Premise
ON tblOpenJobs.ServiceOrderNum = dbo_Premise.AccountNumber)
INNER JOIN dbo_WorkOrders AS w
ON dbo_Premise.PremiseKey = w.PremiseKey)
SET tblOpenJobs.InstallerID = w.InstallerID,
tblOpenJobs.InstallDate = w.InstallDate,
tblOpenJobs.New_Serial_num = w.NewSerial,
tblOpenJobs.Old_Reading = w.OldRead
WHERE (w.InstallDate IN
(SELECT MAX(InstallDate)
FROM dbo_WorkOrders
WHERE dbo_WorkOrders.PremiseKey = w.PremiseKey))
This is correct in syntax, but I'm not sure it can update your data, as multi-table linked update is not easy in Access.

Related

How to format my UPDATE statement correctly? Getting not unique table/alias error

I am getting a "... not unique table/alias 'plots' ..." error when trying to run the following UPDATE statement:
UPDATE homestead.plots
INNER JOIN homestead.graphs
ON homestead.drivers.id = homestead.graphs.driver_id
INNER JOIN homestead.plots
ON homestead.plots.graph_id = homestead.graphs.id
SET homestead.plots.yAxis = homestead.plots.yAxis + 3.4
WHERE homestead.graphs.name = "DI";
Even though the below SELECT statement works fine, and returns the results I want:
SELECT homestead.graphs.driver_id, homestead.drivers.MarketingNo, homestead.graphs.name, homestead.plots.xAxis, homestead.plots.yAxis
FROM homestead.drivers
INNER JOIN homestead.graphs
ON homestead.drivers.id = homestead.graphs.driver_id
INNER JOIN homestead.plots
ON homestead.plots.graph_id = homestead.graphs.id
WHERE homestead.graphs.name = "DI";
Any ideas how to fix my UPDATE statement to work? I've done a lot of research online but cannot understand why this doesn't work.
There are several flaws with your UPDATE statement, for example:
table plots is referenced twice (in the UPDATE and in a JOIN) and not aliased (this is causing the error that you are getting)
you are referring to column id in table homestead.graphs, but this table is not part of any join
Based on your SELECT query, I would try and phrase your UPDATE as follows:
UPDATE homestead.plots p
INNER JOIN homestead.graphs g ON p.graph_id = g.id AND g.name = "DI"
INNER JOIN homestead.drivers d ON d.id = g.driver_id
SET p.yAxis = p.yAxis + 3.4

Mysql Can I make an update with join

I have a database with several tables and all my queries work but I would like to had a new one and can't succeed to make it working.
Two tables 'vins' and 'producteurs'with a foreign key IDproducteur in my 'vins' table.
I need to update the 'vins'table from an excel source where the data are not always written corectly and need to change the stock value for some records of the 'vins' table.The p.nomproducteur and V.annee and v.nom are parameters but for test I put hard values.
My Query is the following:
UPDATE vins AS v
JOIN producteurs p
ON ( p.IDproducteur = v.IDproducteur AND p.nomproducteur LIKE "Charles" )
SET stock = 10
WHERE v.nom LIKE "Symphonie" AND v.annee = 2013
I have two records where the producteur is "Charles" and V.nom is Symphonie one withe annee = 2010 and one with 2013. I got no error in phpMyadmin but there is no results to my Query even by changing some command order.
Is what I want impossible to do?.
Put the condition p.nomproducteur LIKE "Charles" in the WHERE clause:
UPDATE vins v
JOIN producteurs p ON p.IDproducteur = v.IDproducteur
SET stock = 10
WHERE
v.nom = "Symphonie" AND v.annee = 2013 AND p.nomproducteur = "Charles"
Also there is no need for LIKE in this case, a simple = will do.
The update based on JOIN is commonly used in mysql so be sure that your join and where condition really match the values in your tables
UPDATE vins AS v
INNER JOIN producteurs p ON p.IDproducteur = v.IDproducteur
SET v.stock = 10
WHERE v.nom = "Symphonie"
AND v.annee = 2013
AND p.nomproducteur = "Charles"
and do the fact you are not filter for partial string use = instead of like

SQL with 3 criteria using Access

Morning All
I am using Access 2010 and currently have the below SQL which works fine:
Code:
DELETE DISTINCTROW tbl_added.*
FROM tbl_added INNER JOIN tbl_removed ON (tbl_added.SPECIAL_NEED_TYPE = tbl_removed.SPECIAL_NEED_TYPE) AND (tbl_added.NUM_CUST = tbl_removed.NUM_CUST);
I am trying to add another criteria but getting an error:
Code:
DELETE DISTINCTROW tbl_added.*
FROM tbl_added INNER JOIN tbl_removed ON (tbl_added.SPECIAL_NEED_TYPE = tbl_removed.SPECIAL_NEED_TYPE) AND (tbl_added.NUM_CUST = tbl_removed.NUM_CUST) AND (tbl_added.ADDED_REMOVAL_DT < tbl_removed.ADDED_REMOVAL_DT) ;
Error Received:
Could not delete from specified tables
The last criteria I have added is a date
When joining on any operator that is not =, your recordset becomes non-updateable. That means that you can't edit or delete.
You could move all comparisons to the WHERE clause, and use a CROSS JOIN instead, like this:
DELETE DISTINCTROW tbl_added.*
FROM tbl_added, tbl_removed
WHERE (tbl_added.SPECIAL_NEED_TYPE = tbl_removed.SPECIAL_NEED_TYPE) AND (tbl_added.NUM_CUST = tbl_removed.NUM_CUST) AND (tbl_added.ADDED_REMOVAL_DT < tbl_removed.ADDED_REMOVAL_DT) ;
However, that's still not updateable, since a CROSS JOIN is not updateable.
The solution is to keep all = comparisons in the INNER JOIN, and move all other comparisons to the WHERE clause:
DELETE DISTINCTROW tbl_added.*
FROM tbl_added INNER JOIN tbl_removed ON (tbl_added.SPECIAL_NEED_TYPE = tbl_removed.SPECIAL_NEED_TYPE) AND (tbl_added.NUM_CUST = tbl_removed.NUM_CUST)
WHERE (tbl_added.ADDED_REMOVAL_DT < tbl_removed.ADDED_REMOVAL_DT) ;
This keeps the recordset updateable, and still allows you to use a < operator.
In Access SQL you can't join on "<".
You'll have to use a subquery of some kind or create a temp table with those IDs to delete, then join to this.
Edit:
Well, you "can" but not in the GUI designer (see comment from Erik).
So, add the join in the GUI designer the usual way. That will create a join using "=". Then switch to SQL view and change "=" to "<".
It should now run without an error, but you will not be able to switch back to the GUI designer.

MySql convert nested SELECT IN queries into JOINs

I read that using IN in MySql queries causes a slowdown in performance, unlike in Oracle and that a JOIN should be used instead. I am able to convert simple queries, but I am struggling with more complex ones that contain nested SELECTs. For example:
update Records set ActiveStatus=0, TransactionStatus=0, LastUpdated=&
where ActiveStatus!=5 and LastUpdated!=&
and Pk in (select RecordPk from GroupRecordLink
where GroupPk in (select Pk from Groups where ActiveStatus!=5 and
DateTimeStamp>&))
I had a go rewriting it by following this post , but I am not sure my result is correct.
update Records r join (select distinct RecordPk from GroupRecordLink grl join
Groups g on grl.Pk = g.Pk where g.ActiveStatus!=5 and g.DateTimeStamp>&) s
using (Pk) set ActiveStatus=0, TransactionStatus=0, LastUpdated=&
where ActiveStatus!=5 and DateTimeStamp>&
Thanks
Try this:
UPDATE Records
INNER JOIN GroupRecordLink ON Records.Pk = GroupRecordLink.RecordPk
INNER JOIN Groups ON GroupRecordLink.GroupPk = Groups.Pk AND Groups.ActiveStatus != 5
SET Records.ActiveStatus = 0,
Records.TransactionStatus = 0,
Records.LastUpdated = &
WHERE Records.ActiveStatus != 5
AND Records.LastUpdated != &;
In Mysql you could use an explicit join for update (you must change & witha proper value )
update Records
INNER JOIN GroupRecordLink on GroupRecordLink.RecordPk = Records.PK
INNER JOIN Groups on ( GroupRecordLink.GroupPk = Groups.Pk
and Groups.ActiveStatus!=5
and Groups.DateTimeStamp>&)
set ActiveStatus=0,
TransactionStatus=0,
LastUpdated=&

MYSQL get other table data in a join

I am currently running this SQL
SELECT jm_recipe.name, jm_recipe.slug
FROM jm_recipe
LEFT JOIN jm_category_recipe ON jm_category_recipe.recipe_id = jm_recipe.id
WHERE jm_category_recipe.category_id = $cat"
This returns the desired results except that I also need to return the name of the category that the recipe I am looking for is in, to do this I tried to add the field in to my SELECT statement and also add the table into the FROM clause,
SELECT jm_recipe.name, jm_recipe.slug, jm_category_name
FROM jm_recipe, jm_category
LEFT JOIN jm_category_recipe ON jm_category_recipe.recipe_id = jm_recipe.id
WHERE jm_category_recipe.category_id = $cat"
However this just returns no results, what am i doing wrong?
You need to join both tables:
SELECT jm_recipe.name, jm_recipe.slug, jm.category_name
FROM jm_recipe
INNER JOIN jm_category_recipe ON jm_category_recipe.recipe_id = jm_recipe.id
INNER JOIN jm_category ON jm_recipe.recipe_id = jm_category.recipe_id
WHERE jm_category_recipe.category_id = $cat
I've changed the joins to inner joins as well. You might want to make them both LEFT joins if you have NULLs and want them in the result.
Also, you're vulnerable to SQL Injection by simply copying over $cat.
Here's some PHP specific info for you (I'm assuming you're using PHP.)