Syntax Error MySQL Update - mysql

I'm running a pass-through from access to MYSQL and trying to run an update query but keep getting a syntax error. I am brand new to MYSQL. It looks correct to me so I have no clue what I'm doing wrong. The query works in Access.
Access Query:
UPDATE [ProductInformation-ODBC]
INNER JOIN TempPrice
ON [ProductInformation-ODBC].FirstOfSku =TempPrice.FirstOfSku
SET [ProductInformation-ODBC].Price = [TempPrice]![Price];
MYSQL:
UPDATE ProductInformation-ODBC
INNER JOIN TempPrice ON ProductInformation-ODBC.FirstOfSku =
TempPrice.FirstOfSku
SET ProductInformation-ODBC.Price = TempPrice.Price

UPDATE `ProductInformation-ODBC` P
INNER JOIN TempPrice ON P.FirstOfSku =
TempPrice.FirstOfSku
SET P.Price = TempPrice.Price
Don't use - in the table name and choose a proper name for your tables
In the complex queries use an alias name for your tables like P on the code above

Related

MySQL: how to UPDATE query based on SELECT Query

I have following SQL query, that does a bulk update on MySQL server:
UPDATE res_booking AS rbg
SET rbg.host_id = (
SELECT vw.entity_id FROM res_booking AS rb
INNER JOIN pass_single as ps ON (ps.book_id = rb.booking_id)
INNER JOIN voucher_who as vw ON (vw.pass_id = ps.pass_id)
WHERE rb.booking_id = rbg.booking_id
)
WHERE rbg.host_id IS NULL;
Executing it, gives me following error:
Error Code: 1093. You can't specify target table 'rbg' for update in FROM clause
How can be this query be rewritten, so it will work?
I would write this as an update join:
UPDATE res_booking AS rbg
INNER JOIN res_booking AS rb
ON rb.booking_id = rbg.booking_id
INNER JOIN pass_single AS ps
ON ps.book_id = rb.booking_id
INNER JOIN voucher_who AS vw
ON vw.pass_id = ps.pass_id
SET
rbg.host_id = vw.entity_id
WHERE
rbg.host_id IS NULL;
Technically the self join on res_booking should be a left join, because your current update would always assign to every record, regardless of whether or not the subquery returns a value. However, I suspect that inner join logic is what you would really want here.
You can use a correlated subquery. Just leave res_booking out of the subquery:
UPDATE res_booking rbg
SET rbg.host_id = (SELECT vw.entity_id
FROM pass_single ps JOIN
voucher_who vw
ON vw.pass_id = ps.pass_id
WHERE ps.book_id = rbg.booking_id
)
WHERE rbg.host_id IS NULL;
MySQL generates an error if you re-use the table being updated in a subquery. However, it is not needed. Just fix the correlation clause to use the ps table.
Note that this could return an error if the subquery returns more than one row. That is a good thing. If it happens, you will need to figure out how to handle this situation.

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.

SQL How to Update by INNER JOIN -

Please help me figure this out since I tried everything from this forum but still haven't found a solution.
Well, I have two tables:
prices
manufacturers
I want to change the values of two fields that are both in table prices.
And I will just give specific values to those.
The fields are:
prices.override (in which I want to give the value 0) and
prices.product_discount_id (in which I want to give the value 66)
BUT I want to change the fields ONLY FOR the manufacturer with ID 31.
So, I first check that an INNER JOIN works fine.
SELECT manufacturers.manufacturer_id,
prices.product_id,
prices.product_price,
prices.override,
prices.product_discount_id
FROM manufacturers
INNER prices
ON manufacturers.product_id=prices.product_id
AND manufacturers.manufacturer_id=31;
But when I try to update the two fields, I do not know how to make that work.
For example, I tried this but it didn't work:
UPDATE prices
SET prices.override=1
FROM
INNER JOIN prices
ON manufacturers.product_id=prices.product_id
AND manufacturers.manufacturer_id=31;
I also tried this:
UPDATE prices
SET prices.override=1,
INNER JOIN manufacturers
ON prices.virtuemart_product_id = manufacturers.virtuemart_product_id
AND manufacturers.manufacturer_id=31;
What did i do wrong? Usually the error message I get 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 'FROM jos_virtuemart_product_prices prices INNER JOIN jos_virtuemart_product_man' at line 3
I read something for alias but still no result.
Any help would be appreciated!
You have a few syntax problems , MySQL UPDATE..JOIN syntax is :
UPDATE T
JOIN t2 ON()
SET ..
WHERE ..
Secondly, you had an unnecessary comma after the SET prices.override=1, so:
UPDATE prices
INNER JOIN manufacturers
ON prices.virtuemart_product_id = manufacturers.virtuemart_product_id
AND manufacturers.manufacturer_id=31
SET prices.override=1
Try this if you are using SQL Server for mysql above is fine:
UPDATE p SET p.override=1
FROM prices p
INNER JOIN manufacturers ON
p.virtuemart_product_id =manufacturers.virtuemart_product_id
AND manufacturers.manufacturer_id=31;
This is the correct syntax in MySQL:
UPDATE prices p JOIN
manufacturers m
USING (product_id)
SET p.override=1
WHERE m.manufacturer_id = 31;
Note the use of table aliases. These make a query easier to write and to read.
The syntax that you are using is appropriate for SQL Server.
In MySQL the UPDATE with JOIN syntax is different, the SET operator should comes after the JOIN statement.
The correct UPDATE query is
UPDATE prices P
INNER JOIN manufacturers M ON P.virtuemart_product_id = M.virtuemart_product_id
AND M.manufacturer_id = 31;
SET P.override = 1;
UPDATE prices
SET prices.override=1
FROM manufacturers
INNER JOIN prices
ON manufacturers.product_id=prices.product_id
AND manufacturers.manufacturer_id=31;

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.