Subtract value from other table using Inner Join - mysql

I'm trying to subtract a value by another value from another table (Sorry for the mouthful)
But the SQL which i've developed keeps throwing the same error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'FROM products INNER JOIN order_details ON products.ItemID = order_details.Item' at line 2
This is my code:
UPDATE products SET Quantity = (products.Quantity - order_details.Quantity)
FROM products
INNER JOIN order_details
ON products.ItemID = order_details.ItemID
WHERE order_details.OrderID = 95 AND products.ItemID = order_details.ItemID;
Its just code for when someone buys an item, it's supposed to reduce the quantity in the products table by the number of whatever they've bought. Any help is appreciated.

The join is part of the update not of the SETclause:
UPDATE products INNER JOIN order_details
ON products.ItemID = order_details.ItemID
SET Quantity = (products.Quantity - order_details.Quantity)
WHERE order_details.OrderID = 95;

Related

SQL UPDATE 2 table with JOIN

I want to update two tables at the same time, because the connection and update conditions are in the first table. And only simple information like the amount is in the second table
UPDATE
order,
order_product
SET
order.total = order.total*0.00001,
order_product.price = order_product.price*0.00001,
order_product.total = order_product.total*0.00001
FROM
order_product
LEFT JOIN
order ON order_product.order_id = order.order_id
WHERE
order.currency_code = "USD"
AND
order.currency_value = 0.00001000
I keep getting this error
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'FROM
order_product
LEFT JOIN
order ON order_id ...' at line 5
Your syntax is off for MySQL and looks to be taken from some other dialect. For a MySQL update join, use an explicit join as follows:
UPDATE order o
INNER JOIN order_product op
ON op.order_id = o.order_id
SET
o.total = o.total*0.00001,
op.price = op.price*0.00001,
op.total = op.total*0.00001
WHERE
o.currency_code = 'USD' AND
o.currency_value = 0.00001000;

SQL problems with UPDATE SET FROM WHERE statements

I'm very bad at SQL so I'm struggling to UPDATE using values coming from separate tables:
I need use values inside 2 different tables as well as values from PHP script as part of an update query into a third table. with help I was able to make this statement but im getting syntax errors
ou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from
order1
join supplier
on supplier.Order_ID = order1.order_ID
join pr' at line 6
My code is here:
update order1
set Reorder_Time=supplier.Reorder_Time,
Amount_Ordered="123456789",
Stock_Amount=product.Stock_Amount,
from order1
join supplier on supplier.Order_ID = order1.order_ID
join production on supplier.Product_ID = product.Product_ID
where orderId = "123456789"
join production on supplier.Product_ID = product.Product_ID
In this line, write to production.Product_Id instead of product.Product_ID.
or you can use,
from order1 o
inner join supplier sp on sp.Order_ID = o.order_ID
inner join production p on sp.Product_ID = p.Product_ID
where orderId = "123456789"`

How to use Sum and Inner Join in a delete statement

I have a table called TableName which I want to delete from all rows that SUM of their product quantity is less than 2.
I need to inner join table oc_order_product and SUM the values having the same product_id then use this SUM value in where clause to delete all rows with SUM less than 2.
I am using the following query right now:
Delete TablenName from TablenName
INNER JOIN oc_order_product
ON oc_order_product.product_id = TablenName.product_id
where oc_order_product.quantity HAVING SUM(oc_order_product.quantity) < 2;
Which I am getting the following error:
#1064 - You have an error in your SQL syntax; check the manual that
corresponds to your MariaDB server version for the right syntax
to use near 'HAVING SUM(oc_order_product.quantity) < 2' at line 4
Hmmm . . . If you need to do aggregation, you need to do it before the join:
Delete t
from TablenName t join
(select op.product_id, sum(op.quantity) as sumquantity
from oc_order_product op
group by op.product_id
) op
on op.product_id = t.product_id and op.sumquantity < 2;

SQL: SUM and Update query error,

I'm trying to update my riders total ucipoints by following an example, with some small modifications, but I just get an error from it.
example
UPDATE P
SET extrasPrice = t.TotalPrice
FROM BookingPitches AS P INNER JOIN
(
SELECT
PitchID,
SUM(Price) TotalPrice
FROM
BookingPitchExtras
GROUP BY PitchID
) t
ON t.PitchID = p.ID
I got the code from this answer:
SQL Update to the SUM of its joined values
My code looks like this:
UPDATE P
SET ucipoeng = t.TotalPoints
FROM rytterlagsesong AS P INNER JOIN
(
SELECT
rytterid,
SUM(poeng) AS TotalPoints
FROM
t_ucipoeng
WHERE year(dato)='2016'
GROUP BY rytterid
) t
ON t.rytterid = P.rytterid AND t.sesong='2016'
I get the error:
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'FROM rytterlagsesong AS P INNER JOIN ( SELECT rytterid, SUM(ucip' at line 3
Can someone help me find the error?
DB Structure:
rytterlagsesong: rytterid - ucipoeng - sesong
t_ucipoeng: rytterid - dato - poeng
So I want to sum the points (poeng) of all races in 2016 (dato=date) for a rider
And update that riders totalpoint (ucipoeng) for this season (sesong)
The update / join syntax is different per database. For mysql, use this:
UPDATE rytterlagsesong r
INNER JOIN (
SELECT rytterid, SUM(poeng) AS TotalPoints
FROM t_ucipoeng
WHERE year(dato)='2016'
GROUP BY rytterid
) t ON t.rytterid = r.rytterid AND t.sesong='2016'
SET r.ucipoeng = t.TotalPoints

Update SQL with Inner Join in Prestashop in DB

I try to update the quantity in a Prestashop table. I have an INNER JOIN to get the upc from the table "ps_product_attribute"
UPDATE ps_stock_available
SET ps_stock_available.quantity = ps_stock_available.quantity - 1
INNER JOIN ps_product_attribute ON ps_product_attribute.id_product_attribute = ps_stock_available.id_product_attribute
WHERE ps_product_attribute.ups = 01900000118;
But I always have this error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds
to your MySQL server version for the right syntax to use near 'INNER
JOIN ps_product_attribute ON ps_product_attribute.id_product_attribute = p' at line 3
Someone can help me? I have try lot of different thing.
EDIT:
I have try a different way:
With a SELECT, I can have the id_product_attribute from ps_stock_available.
SELECT id_product_attribute
FROM ps_product_attribute
WHERE upc in ("01900000118","01900000119");
Does it possible to do an update with the result?
UPDATE ps_stock_available
SET quantity = quantity-1
WHERE id_product_attribute in ("result1", "result2");
I found the solution !
SELECT sa.id_product, sa.id_product_attribute, sa.quantity, pa.upc
FROM ps_stock_available AS sa
LEFT OUTER JOIN ps_product_attribute AS pa ON pa.id_product_attribute = sa.id_product_attribute
WHERE sa.id_product = 140;