Update table from View - ms-access

I have the following view called cont_struct_breaks:
pipe_segment_reference com_struct_score com_defects com_struct_index
2610 353 111 3.2
3988 266 97 2.7
5632 49 22 2.2
I have another table called structural_rating with fields
pipe_segment_reference, structural_score structural_defects, structural_index
I want to UPDATE values in "structural_rating" from "cont_struct_breaks" WHERE the pipe_segment_reference matches.
I cannot figure this out however, I tired doing something like this but no luck.
Attempted This
[EDIT]
The cont_struct_breaks view is create with a select statement which looks like this:
SELECT
structural_rating.Pipe_Segment_Reference,
cont_struct + structural_score AS com_struct_score,
cont_struct_d + structural_defects AS com_defects,
ROUND(com_struct_score / com_defects, 1) AS com_struct_index
FROM ...
UPDATE structural_ratings table from the select statement above if easier than the view.

Doing a straight-up replace of your info for what's in the answer at that linked answer, I got this:
UPDATE
structural_rating
SET
structural_rating.structural_score = RAN.com_struct_score
FROM
structural_rating SI
INNER JOIN
cont_struct_breaks RAN
ON
SI.pipe_segment_reference = RAN.pipe_segment_reference
You'll probably want to change the names of the aliases to reduce confusion, but it'll work just fine the way it is.
So now you want to update additional fields. When you update multiple fields at once, you just separate them with commas:
SET
structural_rating.structural_score = RAN.com_struct_score,
structural_rating.structural_defects = RAN.com_defects,
structural_rating.structural_index = RAN.com_struct_index
So, your complete query should be:
UPDATE
structural_rating
SET
structural_rating.structural_score = RAN.com_struct_score,
structural_rating.structural_defects = RAN.com_defects,
structural_rating.structural_index = RAN.com_struct_index
FROM
structural_rating SI
INNER JOIN
cont_struct_breaks RAN
ON
SI.pipe_segment_reference = RAN.pipe_segment_reference

I ended up doing this writing to new table instead of view then running this:
UPDATE structural_rating
INNER JOIN cont_struct
ON structural_rating.pipe_segment_reference = cont_struct.pipe_segment_reference
SET
structural_score = cont_struct.com_struct_score,
structural_defects = cont_struct.com_defects,
structural_index = cont_struct.com_struct_index;

Related

Update table based on value inside this table

I'm trying to update a tablecolumn based on another value inside this table but I keep getting an error.
My query is as follows:
UPDATE partcreditor
SET partcreditor.creditorid = creditor.creditorid
FROM partcreditor
INNER JOIN creditor ON partcreditor.creditornr = creditor.creditornr
WHERE creditor.relgroupid = 1
AND creditor.creditortypeid = 1
UPDATE partcreditor AS PC
INNER JOIN creditor AS CR ON PC.creditornr = CR.creditornr
SET PC.creditorid = CR.creditorid
WHERE CR.relgroupid = 1 AND CR.creditortypeid = 1
No need to use the FROM clause in the update. As well as use alias name for better readability.

Delete row if exists

DELETE IF EXIST `#__menu`.*
FROM `#__menu`
LEFT JOIN `#__extensions` ON `#__extensions`.`name` = 'com_view'
WHERE `#__menu`.`component_id` = `#__xtensions`.`extension_id`
AND `#__menu`.`alias` = 'view-sites' AND `#__menu`.`path` = 'view-sites' AND `#__menu`.`title` = 'View sites';
What is wrong in my sql? I think the problem is in IF EXIST, but i could not figure out how to use it on row.
When you're deleting rows from a table, you don't need to use IF EXISTS - you're using a WHERE clause, so if it exists - it will be deleted.
Try changing your query to:
DELETE
FROM `#__menu`
LEFT JOIN `#__extensions` ON `#__extensions`.`name` = 'com_view'
WHERE `#__menu`.`component_id` = `#__xtensions`.`extension_id`
AND `#__menu`.`alias` = 'view-sites' AND `#__menu`.`path` = 'view-sites' AND `#__menu`.`title` = 'View sites';
Also, you don't need to specify ```#__menu.*`` (the columns) to be deleted - you'll just needDELETE FROM...`. Check out here for more info regarding the syntax.

SQL Update Join error

I have had a read through a few of the other issues around joining on SQL Updates but haven't been able to finalise a query.
System is all in MySQL (INNODB table structure)
We are wanting to update an amount in one table that will increase an amount based on 2 variables from another table. There are a few constraints that need to be checked in the update to make sure the variables from the second table match the keys in the table to be updated
UPDATE as1
SET as1.amount = as1.amount + (b1.workers * b1.level)
FROM account_stock AS as1
INNER JOIN building AS b1 ON as1.accountID = b1.accountID
INNER JOIN building_seed AS bs1 ON bs1.buildingID = b1.buildingID
WHERE bs1.stockID = as1.stockID
AND b1.accountID = as1.accountID
AND b1.locID = as1.locID
AND b1.status = active
AND b1.gTime > 0
It's getting an error and I can't pick it. Sorry if it is a simple question, all my SQL is self taught so some of the habits I have aren't very good!
MySQL syntax for UPDATE is different. There is no FROM:
UPDATE
account_stock AS as1
INNER JOIN building AS b1 ON as1.accountID = b1.accountID
INNER JOIN building_seed AS bs1 ON bs1.buildingID = b1.buildingID
SET as1.amount = as1.amount + (b1.workers * b1.level)
WHERE bs1.stockID = as1.stockID
AND b1.locID = as1.locID
AND b1.status = active
AND b1.gTime > 0 ;
--- removed duplicate :
--- AND b1.accountID = as1.accountID
Also: is active a column or you meant to write?: AND b1.status = 'active'

Update a single table based on multiple tables - appears to works MySql not ACCESS 2003

Mysql Version Works
UPDATE results SET rCARRIER = (
SELECT cellCarrierName
FROM tblImportedTempTable, user, cellCarrier
WHERE
userEmployeeNumber = tblImportedTempTable.EMPLOYEENUMBER
AND userId = results.rUserId
AND results.rPHONENUMBER = tblImportedTempTable.PHONENUMBER
AND CARRIER = cellCarrierId )
I have written this sql that works fine in MySql(above) and fails in access 2003(below) any suggestions? Is one or both of the 2 nonstandard sql? Does Access hav an admin problem?
Sorry the field and table names are diferent this is the ACCESS version.
Access version
UPDATE tblWorkerPhoneNumber SET tblWorkerPhoneNumber.PhoneCarrier = (
SELECT PhoneCarrierType.CarrierName
FROM tblImportedPhoneCarrier, tblWorkerMaster, PhoneCarrierType
WHERE
tblWorkerMaster.EmployeeNumber = tblImportedPhoneCarrier.Emp
AND tblWorkerMaster.WorkerID = tblWorkerPhoneNumber.WorkerID
AND tblWorkerPhoneNumber.PhoneNumber = tblImportedPhoneCarrier.Cell
AND tblImportedPhoneCarrier.CarrierCode = PhoneCarrierType.CarrierID )
Error Message
Operation must use and updateable query
Thanks
In MS Access, something like this:
UPDATE tblWorkerPhoneNumber
INNER JOIN tblWorkerMaster ON tblWorkerMaster.WorkerID = tblWorkerPhoneNumber.WorkerID
INNER JOIN tblImportedPhoneCarrier ON tblWorkerPhoneNumber.PhoneNumber = tblImportedPhoneCarrier.Cell
INNER JOIN PhoneCarrierType ON tblImportedPhoneCarrier.CarrierCode = PhoneCarrierType.CarrierID
SET tblWorkerPhoneNumber.PhoneCarrier = PhoneCarrierType.CarrierName
WHERE tblWorkerMaster.EmployeeNumber = tblImportedPhoneCarrier.Emp
(Might need to change the join conditions; I'm not familiar with your schema)

How use table column as string in a multi-table update in MySQL?

Two tables,
agent(agent_id, agent_real_name, .....)
blog(blog_id, blog_agent_id, blog_name, ...)
Now I want to set the blog_name as agent_real_name + "'s blog"
I used following SQL sentence but failed,
update blog, agent set blog_name = agent_real_name '\'s blog' where agent_id = 31
PS: 31 is the id of a agent
What's wrong?
Thanks.
UPDATE blog b INNER JOIN agent a
ON a.agent_id = b.blog_agent_id
SET b.blog_name = CONCAT(a.agent_real_name,'\'s blog')
WHERE a.agent_id = 31
try to use:
update blog, agent set blog_name = concat(agent_real_name, '\'s blog') where agent_id = 31