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'
Related
I'm trying to update records through a single inner join with multiple criteria. My best effort so far is this:
UPDATE FormData d
INNER JOIN ProductGrowthDays g
ON d.ProductCode = g.ProductCode AND
ON d.ProductionLineCode = g.ProductionLineCode AND
ON g.MonthIndex = MONTH(d.SowingDate)
SET d.EstimatedDays = g.GrowingDays
WHERE
d.EventTypeId = 1
Access gives the error 'Syntax error (missing operator)' and highlights the 'r' in 'd.ProductCode'. The join is guaranteed to give a single row.
Could anyone give me pointers on how to fix this?
D'oh. The answer was as follows:
UPDATE FormData d
INNER JOIN ProductGrowthDays g
ON (d.ProductCode = g.ProductCode
AND d.ProductionLineCode = g.ProductionLineCode
AND g.MonthIndex = MONTH(d.SowingDate))
SET d.EstimatedDays = g.GrowingDays
WHERE
d.EventTypeId = 1
I was sure I tried that at one point, but obviously not. Well, leaving this here if someone else should need it.
i'm trying to update column from another table which means :
SELECT DISTINCT id FROM creature WHERE map = 389;
This SQL will give me this result :
11323
11322
11324
11520
11321
What i want is to update creature_template lootid = 11323 where entry = 11323 so it goes as following :
UPDATE creature_template SET lootid = 11323 WHERE entry = 11323
I have tried this :
UPDATE creature_template SET lootid =
(SELECT DISTINCT id
FROM creature
WHERE map = 389) WHERE lootid = entry;
I'm sure it's incorrect simply it's not logic but couldn't find the logical answer for this.
Even REPLACE could work instead of UPDATE so any will work.
You need JOIN with UPDATE :
UPDATE creature_template ct
INNER JOIN creature c
ON c.id = ct.entry
SET ct.lootid = c.id
WHERE c.map = 389;
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.
I've got two Queries to Update two tables:
First Table
UPDATE user_info SET `location` = ".$locationid.", `looking_for` = ".$lookingfor." WHERE `user_info`.`user_id` = ".$infoid.";
Second Table
UPDATE user_personality SET `personality` = '".$changedescription."' WHERE `user_personality`.`user_info_id` = ".$infoid.";
And I'm trying to merge those two Queries, using the same statement.
UPDATE user_info, user_personality
SET user_info.location = ".$locationid.", user_info.`looking_for` = ".$lookingfor.", user_personality.personality = '".$changedescription."'
WHERE `user_info`.`user_id` = ".$infoid."
AND `user_personality`.`user_info_id` = ".$infoid."
I'm not receiving any error message, but is not updating.
What am I doing wrong?
Thanks.
Just a guess...
"
UPDATE user_info i
JOIN user_personality p
ON p.user_info_id = i.user_id
SET i.location = $locationid
, i.looking_for = '$lookingfor'
, p.personality = '$changedescription'
WHERE i.user_id = $infoid;
";
If you set the 2 table fields equal to each other in the where clause it should work, so I believe you'd change your where clause to:
WHERE `user_info`.`user_id` = `user_personality`.`user_info_id`
AND `user_info`.`user_id` = ".$infoid."
MySQL definitely supports updating multiple tables, so the where clause that works for a multi table select statement should also work for an update.
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.