SQL problems with UPDATE SET FROM WHERE statements - mysql

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"`

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;

Subtract value from other table using Inner Join

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;

mysql update with several joins

I would like to update with 2 join.... but :
UPDATE glpi.glpi_users
FROM
glpi.glpi_groups_users
INNER JOIN glpi.glpi_groups ON glpi.glpi_groups_users.groups_id = glpi.glpi_groups.id
INNER JOIN glpi.glpi_users ON glpi.glpi_users.id = glpi.glpi_groups_users.users_id
SET glpi.glpi_users.id = 2
WHERE
glpi.glpi_groups.`name` LIKE 'technique'
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
glpi.glpi_groups_users
INNER JOIN glpi.glpi_groups ON glpi.glpi_groups_us' at line 2
Time: 0s
thanks for your help
I think that the syntax you want is:
UPDATE glpi.glpi_users u
INNER JOIN glpi.glpi_groups_users gu ON gu.users_id = u.id
INNER JOIN glpi.glpi_groups g ON g.id = gu.groups_id
SET u.id = 2
WHERE g.name = 'technique'
That is: MySQL update/join syntax doesn't have a FROM clause - it looks like UPDATE ... JOIN ... SET ... WHERE ....
Notes:
since no wildcards do appear in the right side operand, name LIKE 'technique' is equivalent to name = 'technique'
table aliases make the query easier to write and read

SQL query- Different databases with inner join

I have two DBs- RATINGSAPP and MIGRATIONDATA.
I want to update a table in RATINGSAPP with some values in a table in MIGRATIONDATA. I am trying to run this query:
update r set internal_id = m.internal_id from ratingsapp.hotel03 as r
inner join migrationdata.migration as m on r.hotel_id = m.restaurant_id
This gives me error:
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 ratingsapp.hotel03 as r inner join migrationdata.migration as m on r.hotel_' at line 1
But a similar select query works for me and gives proper results.
select r.hotel_id, m.internal_id from ratingsapp.hotel03 as r
inner join migrationdata.migration as m on r.hotel_id = m.restaurant_id
What I am doing wrong in the update query?
The correct MySQL syntax is:
update ratingsapp.hotel03 r inner join
migrationdata.migration as m
on r.hotel_id = m.restaurant_id
set internal_id = m.internal_id ;
There is no from clause in a MySQL update. You are using SQL Server/Postgres syntax.

Mysql multiple Inner Joins not working

I have following query that is working fine in TSQL
SELECT
shoppingcart_1.price, shoppingcart_1.stid, course.isbn,
book.BookTitle, course.Course_ID, schedule.stid AS Expr1
FROM
book
INNER JOIN
shoppingcart AS shoppingcart_1
INNER JOIN
schedule ON shoppingcart_1.cid = schedule.course_ID
INNER JOIN
course ON schedule.course_ID = course.Course_ID
ON book.isbn = course.isbn
WHERE
(shoppingcart_1.stid = '20070004')
but when I run it in Mysql it displays error on line
INNER JOIN course ON schedule.course_ID = course.Course_ID ON book.isbn = course.isbn
Error text is
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 'ON book.isbn = course.isbn WHERE (shoppingcart_1.stid =
'20070004') LIMIT 0' at line 6
I am writing queries first time in mysql, please help
Seems like you have your joins a little mixed up. I tried cleaning it up. See if this works for you
SELECT shoppingcart_1.price, shoppingcart_1.stid, course.isbn, book.BookTitle, course.Course_ID, schedule.stid AS Expr1
FROM book
INNER JOIN course ON book.isbn = course.isbn
INNER JOIN schedule ON course.Course_ID = schedule.course_ID
INNER JOIN shoppingcart AS shoppingcart_1 ON schedule.course_ID = shoppingcart_1.cid
WHERE shoppingcart_1.stid = '20070004'
Note how I join book to course, then course to schedule, then schedule to shoppingcart. Then use the WHERE clause to specify other conditions.