UPDATE with INNER JOIN phpmyadmin - mysql

UPDATE icmsc100 SET codigo='test'
FROM icmsc100
INNER JOIN icms307
ON icms307.ajusteDeCodigo = icmsc100.codigoItem;
I'm trying to use it this way, but it's returning an error, how should it be done?
Error #1064 - Syntax Error

You can update the data with the Inner Join In MYSQL as below.
UPDATE icmsc100
INNER JOIN icms307
ON icms307.ajusteDeCodigo = icmsc100.codigoItem
SET codigo='test';

Related

One table update accoriding to another table MySQL update error

I have two tables name activties and post_media now I want to update the media background color in activities table according to post media table record but when I run query it give me error.
Query
UPDATE A
SET A.bg_color = M.bg_color
FROM activities A
INNER JOIN post_media M ON A.relation_id = M.user_post_id AND A.media=M.file
WHERE A.relation_id>0
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 'FROM activities A INNER JOIN post_media M ON A.relation_id =
M.user_post_' at line 3
UPDATE syntax is different from SELECT. There is no FROM clause usage in UPDATE statement.
General flow is: UPDATE <table name> [JOIN <other tables>] SET ...
UPDATE activities A
INNER JOIN post_media M ON A.relation_id = M.user_post_id AND A.media=M.file
SET A.bg_color = M.bg_color
WHERE A.relation_id>0
Check documentation here for full syntax and further understanding: https://dev.mysql.com/doc/refman/8.0/en/update.html
Update query with use of join is different than SELECT query. Here you need to add tables before SET clause and all conditions in WHERE clause like SELECT.
e.g/
UPDATE t1, t2
SET t1.field = t2.field
WHERE condition 1
AND condition 2
So your query will be like as below:
UPDATE activities A, post_media M
SET A.bg_color = M.bg_color
WHERE A.relation_id = M.user_post_id
AND A.media=M.file
AND A.relation_id>0
Try this one.

UPDATE USING PHPMYADMIN

Good Afternoon,
I am trying to update a table using price data from another table however I get an error using an inner join. I am sure its something very stupid but having spent the best part of my day on this its time to ask for help.
If I do the following SELECT statement to test my inner join syntax works as it should
SELECT *
FROM polaracc_osrs_property_field_value
INNER JOIN polaracc_osrs_properties
ON polaracc_osrs_property_field_value.pro_id = polaracc_osrs_properties.id
WHERE polaracc_osrs_property_field_value.field_id =112
However when I then try and run an update statement using the price from one table to populate the 2nd I get the below 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 'FROM polaracc_osrs_property_field_value INNER JOIN
polaracc_osrs_properties ' at line 3
The syntax used for the update statement is below
UPDATE polaracc_osrs_property_field_value
SET polaracc_osrs_property_field_value.value_integer = polaracc_osrs_properties.price
FROM polaracc_osrs_property_field_value
INNER JOIN polaracc_osrs_properties
ON polaracc_osrs_property_field_value.pro_id = polaracc_osrs_properties.id
WHERE polaracc_osrs_property_field_value.field_id = 112
Your join needs to happen before you set your values like this:
UPDATE polaracc_osrs_property_field_value
INNER JOIN polaracc_osrs_properties
ON polaracc_osrs_property_field_value.pro_id = polaracc_osrs_properties.id
SET polaracc_osrs_property_field_value.value_integer = polaracc_osrs_properties.price
WHERE polaracc_osrs_property_field_value.field_id = 112;
Hope this helps.

MySql update syntax error from inner join

UPDATE
`universities`
SET
`universities`.countryid = `countries`.id,
FROM
`universities`
INNER JOIN
`countries`
ON
`universities`.country = `countries`.name
When I try to run the sql statements above via PhpMyAdmin, it would give syntax errors. I wrote the statements based on this answer.
This is the correct syntax in MySQL:
UPDATE universities u JOIN
countries c
ON u.country = c.name
SET u.countryid = c.id;
In addition, I introduced table aliases (so the query is easier to write and to read) and removed an extraneous comma.

syntax error in update with inner join query mysql

My Query :
UPDATE i SET i.CurStock = i.CurStock-g.Qty
FROM inv_inventarymaster AS i INNER JOIN inv_goodsissue AS g
ON i.ItemName = g.ItemName WHERE g.DATE='2014-03-20';
Error:
You have an error in your SQL syntax; check the manual that correspond
to your MySQL server version for the right syntax to use near 'FROM
inv_inventarymaster as i INNER JOIN inv_goodsissue as g ON ' at line 1
Kindly help me getting the right syntax.
Try this ...Not Tested but as MYsql http://dev.mysql.com/doc/refman/5.0/en/update.html
UPDATE inv_inventarymaster AS i INNER JOIN inv_goodsissue AS g SET
i.CurStock = i.CurStock-g.Qty
WHERE i.ItemName = g.ItemName and g.DATE='2014-03-20';
its not a right thing to use FROM in Mysql UPDATE query.
You can use the query like this
UPDATE inv_inventarymaster AS i
INNER JOIN inv_goodsissue AS g ON i.ItemName = g.ItemName
SET i.CurStock = i.CurStock-g.Qty
WHERE g.DATE='2014-03-20';

Update table column with inner join

UPDATE micro_finance_loans AS 'ML' SET ML.Loan_status=11
INNER JOIN micro_finance_customers AS 'MC' on MC.Customer_Id=ML.Customer_Id
WHERE ML.loan_status=10 AND MC.Number='4410188992243'
Can someone please tell me whats the error in this query, error as:
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 microfinance_customers as mc on mc.CustomerId=ml.CustomerId
where ml' at line 2
UPDATE micro_finance_loans AS ML
INNER JOIN micro_finance_customers AS MC on MC.Customer_Id=ML.Customer_Id
SET ML.Loan_status=11
WHERE ML.loan_status=10 AND MC.Number='4410188992243'
Try this instead
UPDATE micro_finance_loans AS 'ML' SET ML.Loan_status=11 from micro_finance_loans
INNER JOIN micro_finance_customers AS 'MC' on MC.Customer_Id=ML.Customer_Id
WHERE ML.loan_status=10 AND MC.Number='4410188992243'