Update Quantity to subtract 1 - mysql

Update product.tblproductinformation
SET Quantity = (Quantity-1)
where(Select iProduct.ProductID
from tblindividualproduct as iProduct
INNER JOIN tblproductinformation as pInfo ON iProduct.Code = pInfo.Code) = #p1"
i want to update my quantity to subtract 1. I also included inner join because my where is in another table. i got an error:
You cant specify target table 'tblproductinformation' for update in FROM clause
what's wrong?

You can use the multiple-table UPDATE syntax to join the tables directly:
UPDATE tblproductinformation AS pInfo
JOIN tblindividualproduct AS iProduct ON iProduct.Code = pInfo.Code
SET pInfo.Quantity = pInfo.Quantity - 1
WHERE iProduct.ProductID = #p1

Related

UPDATE random result numbers to same numbers result

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;

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.

Can't update two tables in one Query

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.

Multiply in update subquery select

I'm trying to update a table based on 2 select subquery that will be multipied to produce the value for Harga column
here is my code :
UPDATE bahanmakanan
SET Harga = (SELECT HargaSatuan from detail_bahanmakanan
WHERE IDBahanMakanan = "BM01")* (SELECT jumlah from bahanmakanan
WHERE IDBahanMakanan = "BM01")
WHERE IDBahanMakanan = "BM01" ;
The error message return
Error Code: 1093. You can't specify target table 'bahanmakanan' for update in FROM clause
you can simply do this using JOIN,
UPDATE bahanmakanan a
INNER JOIN detail_bahanmakanan b
ON a.IDBahanMakanan = b.IDBahanMakanan
SET a.Harga = a.jumlah * b.HargaSatuan
WHERE a.IDBahanMakanan = 'BM01'
Please do backup first your database before executing the statement.
Try this:
UPDATE bahanmakanan as t1
JOIN detail_bahanmakanan as t2 USING(IDBahanMakanan)
SET t1.Harga = t2.HargaSatuan * t1.jumlah
WHERE IDBahanMakanan = "BM01";

Update Query Issues

Here is my update query.
UPDATE sugarcrm.qb_salesorders_leads_c, sugarcrm.qb_salesorders
SET sugarcrm.qb_salesorders_leads_c.qb_salesorders_leadsleads_ida = sugarcrm.qb_salesorders.memo, sugarcrm.qb_salesorders_leads_c.qb_salesorders_leadsqb_salesorders_idb = sugarcrm.qb_salesorders.id
WHERE sugarcrm.qb_salesorders.id = sugarcrm.qb_salesorders_leads_c.qb_salesorders_leadsqb_salesorders_idb
When I run this it gives me Affected rows 0.
Here is my select statement using the same info that is in the WHERE statement.
SELECT * from qb_salesorders_leads_c, sugarcrm.qb_salesorders
WHERE sugarcrm.qb_salesorders_leads_c.qb_salesorders_leadsqb_salesorders_idb = sugarcrm.qb_salesorders.id
This returns 354 Rows which is what I am expecting to update on the update query. What am I missing. Please help!
Convert the implicit join to an explicit one:
UPDATE sugarcrm.qb_salesorders_leads_c leads
INNER JOIN sugarcrm.qb_salesorders orders
ON orders.id = leads.qb_salesorders_leadsqb_salesorders_idb
SET leads.qb_salesorders_leadsleads_ida = orders.memo,
leads.qb_salesorders_leadsqb_salesorders_idb = orders.id
As you can see, I also used aliases to make the SQL compact and legible.
To UPDATE using a join, you have to use the explicit join syntax:
UPDATE sugarcrm.qb_salesorders_leads_c
INNER JOIN sugarcrm.qb_salesorders
ON qb_salesorders_leadsqb_salesorders_idb.id
= sugarcrm.qb_salesorders_leads_c.qb_salesorders_leadsqb_salesorders_idb
SET sugarcrm.qb_salesorders_leads_c.qb_salesorders_leadsleads_ida
= qb_salesorders_leadsqb_salesorders_idb.memo
, sugarcrm.qb_salesorders_leads_c.qb_salesorders_leadsqb_salesorders_idb
= sugarcrm.qb_salesorders.id