Update a table by joining 3 tables - mysql

i have 3 tables:
tblproduct (pro_Id, qty, unitprice)
tblorderdetails (order_id, pro_Id, qty)
tblorder (order_id, totalAmount)
i intend joining these tables to as to update the totalAmount in the tblorder table. This is my query using MySql console:
UPDATE o
SET o.totalAmount = p.unitprice * d.qty
FROM tblorder o INNER JOIN tblorderdetails d
on o.order_id = d.order_id
INNER JOIN tblproduct p
on p.pro_Id = d.pro_Id;
This is the error i get:
ERROR 1064 (42000): 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 'from tblorder o inner join tblorderdetails d

The syntax is not correct and it should be as
UPDATE tblorder o
INNER JOIN tblorderdetails d
on o.order_id = d.order_id
INNER JOIN tblproduct p
on p.pro_Id = d.pro_Id
SET o.totalAmount = p.unitprice * d.qty ;

Related

SQL update table using result of another query

I am trying to update the table(orders)after calculate the total amount of that order, I successfully calculate the total amount, but I cannot find way to update the table using the result.
code I successfully calculate the total amount:
SELECT orders.orderid,
SUM(ordersdish.quantity*dish.price) AS total
FROM dish
JOIN ordersdish
ON ordersdish.dishid = dish.dishid
JOIN orders
ON orders.orderid = ordersdish.orderid
GROUP BY orders.orderid;
Result:
total amount
Code I tried to update table(orders):
UPDATE orders
SET total = t1.total
FROM (
SELECT orders.orderid,
SUM(ordersdish.quantity*dish.price) AS total
FROM dish
JOIN ordersdish
ON ordersdish.dishid = dish.dishid
JOIN orders
ON orders.orderid = ordersdish.orderid
GROUP BY orders.orderid
)t1
WHERE orders.orderid = t1.orderid;
MySQL said:
#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
(select orders.orderid, SUM(ordersdish.quantity*dish.price)as total
from ' at line 3
And here is the table "orders":
The correct syntax in MySQL use JOIN:
update orders o join
(select od.orderid, SUM(od.quantity * d.price)as total
from dish d join
ordersdish od
on od.dishid = d.dishid
group by od.orderid
) t1
on o.orderid = t1.orderid
set o.total = t1.total;
Note that orders is not needed in the subquery, because the orderid is in orderdish.
Please try this answer.
UPDATE o
SET o.total = t1.total
(SELECT o.orderid, SUM(od.quantity * d.price) AS total
FROM dish AS d
JOIN ordersdish AS od ON od.dishid = d.dishid
JOIN orders AS o ON o.orderid = od.orderid
GROUP BY orders.orderid) t1
WHERE o.orderid = t1.orderid;

Getting error when creating a view from 3 tables

I have to create a view in MySQL, this is the code:
CREATE VIEW dashboard_sales AS (
SELECT o.order_id,o.order_date,o.order_status,op.op_status,oi.oi_qty
FROM
order o
LEFT JOIN
order_items oi
ON
o.order_id = oi.order_id
LEFT JOIN
order_payment op
ON
o.order_id = op.order_id
GROUP BY o.order_id
);
but when I execute in phpmyadmin, I get an 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 'order o
LEFT JOIN
order_items oi
ON
o.order_id = oi.' at line 4
How to solve this problem?
You need a backtick for order
CREATE VIEW dashboard_sales AS (
SELECT o.order_id,o.order_date,o.order_status,op.op_status,oi.oi_qty
FROM `order` o LEFT JOIN order_items oi
ON o.order_id = oi.order_id
LEFT JOIN order_payment op
ON o.order_id = op.order_id
);
When you use the keywords for the name of the tables, you should place the table between the Brackets.
CREATE VIEW dashboard_sales AS (
SELECT o.order_id,o.order_date,o.order_status,op.op_status,oi.oi_qty
FROM
[order] o
LEFT JOIN
order_items oi
ON
o.order_id = oi.order_id
LEFT JOIN
order_payment op
ON
o.order_id = op.order_id
GROUP BY o.order_id
);

Update and select query in mysql query

I am using the below format to use select and update command in the same query.
UPDATE t
SET t.col1 = o.col1
FROM table1 AS t
INNER JOIN
table2 AS o
ON t.id = o.id
I applied the same concept in my query but it is throwing an error which I am not able resolve. Any idea where I am going wrong here?
update T set T.price = 2*OT.ingredients from Cake as T Inner join (select
A.cakeid, B.price, sum(C.price) ingredients
from
Contain as A
inner join
Cake as B ON A.cakeid = B.cakeid
inner join
Ingredient as C ON C.ingredid = A.ingredid
group by A.cakeid
having B.price <= 2 * sum(C.price) ) as OT on OT.cakeid = T.cakeid
Error Code: 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 'from Cake as T Inner join (select A.cakeid, B.price, sum(C.price) ingredien' at line 1
The correct syntax in MySQL is:
update Cake T Inner join
(select A.cakeid, B.price, sum(C.price) ingredients
from Contain A inner join
Cake B
ON A.cakeid = B.cakeid inner join
Ingredient as C
ON C.ingredid = A.ingredid
group by A.cakeid
having B.price <= 2 * sum(C.price)
) OT
on OT.cakeid = T.cakeid
set T.price = 2*OT.ingredients ;

Another 1064 syntax error

I just can't figure out what's wrong with this syntax. I get a #1064 syntax error at the last line.
SELECT b.id, b.lastname, b.name, c.balance, a.maxdebt, b.warndata, b.warndownload, b.warnupload, b.warndebt, b.cutoffdata, b.cutoffdownload, b.cutoffupload, b.cutoffdebt, b.data, b.download, b.upload, b.warning, b.access, b.cutoffstop
FROM (
SELECT customers.id AS id, SUM(tariffs.value) AS maxdebt
FROM tariffs
INNER JOIN assignments ON tariffs.id = assignments.tariffid
INNER JOIN customers ON assignments.customerid = customers.id
GROUP BY id
) a
LEFT JOIN (
SELECT customers.id AS id, UPPER(lastname) AS lastname, customers.name AS name, SUM(stats.upload+stats.download) AS data, SUM(stats.download) AS download, SUM(stats.upload) AS upload, cutoffstop, warndata, warndownload, warnupload, warndebt, cutoffdata, cutoffdownload, cutoffupload, cutoffdebt, warning, access
FROM customers
LEFT JOIN nodes ON customers.id = nodes.ownerid
LEFT JOIN stats ON nodes.id = stats.nodeid
LEFT JOIN customerwarnings ON customers.id = customerwarnings.id
GROUP BY id
) b
LEFT JOIN (
SELECT customerid, SUM(cash.value) AS balance
FROM cash
GROUP BY customerid
) c ON a.id = b.id = c.customerid
I've tried to join the tables with RIGHT and LEFT JOINS to see if there is a difference, but they give me the same error.
The different queries within work fine on themselves, but when I join them together in this query I get a syntax error. Does anyone have an idea what I should or shouldn't do?
The 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 '' at line 21"
Here is the reference for Multiple LEFT JOIN
SELECT b.id, b.lastname, b.name, c.balance, a.maxdebt, b.warndata, b.warndownload, b.warnupload, b.warndebt, b.cutoffdata, b.cutoffdownload, b.cutoffupload, b.cutoffdebt, b.data, b.download, b.upload, b.warning, b.access, b.cutoffstop
FROM (
SELECT customers.id AS id, SUM(tariffs.value) AS maxdebt
FROM tariffs
INNER JOIN assignments ON tariffs.id = assignments.tariffid
INNER JOIN customers ON assignments.customerid = customers.id
GROUP BY id
) a
LEFT JOIN (
SELECT customers.id AS id, UPPER(lastname) AS lastname, customers.name AS name, SUM(stats.upload+stats.download) AS data, SUM(stats.download) AS download, SUM(stats.upload) AS upload, customers.cutoffstop, warndata, warndownload, warnupload, warndebt, cutoffdata, cutoffdownload, cutoffupload, cutoffdebt, nodes.warning, nodes.access
FROM customers
LEFT JOIN nodes ON customers.id = nodes.ownerid
LEFT JOIN stats ON nodes.id = stats.nodeid
LEFT JOIN customerwarnings ON customers.id = customerwarnings.id
GROUP BY id
) b ON a.id = b.id
LEFT JOIN (
SELECT customerid, SUM(cash.value) AS balance
FROM cash
GROUP BY customerid
) c ON a.id = c.customerid

phpmyadmin update query using 3 tables in giving error

I am getting the following Error
064 - 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 'from catalog_product_flat_1 a inner join
catalog_product_entity c on a.sku' at line 3
when I try out the below query on phpmyadmin
update a
set a.small_image = b.value
from `catalog_product_flat_1` a
inner join `catalog_product_entity` c
on a.sku = c.sku
inner join `catalog_product_entity_media_gallery` b
on b.entity_id = c.entity_id
I also tryout without using alias but still same issue
The SET clause comes after the JOIN clauses:
UPDATE `catalog_product_flat_1` a
inner join `catalog_product_entity` c
on a.sku = c.sku
inner join `catalog_product_entity_media_gallery` b
on b.entity_id = c.entity_id
SET a.small_image = b.value
See the documenation:
http://dev.mysql.com/doc/refman/5.5/en/update.html