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.
Related
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.
While trying to execute the query given below, I get an syntax error
I need to update a column value from table civicrm_address and move it from abc_abc_drupal_civi_4_17 database to abc_drupal database
In order to achieve it, I get an syntax error near from FROM,
The error I get is as follows
#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 abc_abc_drupal_civi_4_17.civicrm_address,abc_drupal.civic' at line 3
How can I fix it?
UPDATE abc_drupal.civicrm_address
SET abc_drupal.civicrm_address.state_province_id = abc_abc_drupal_civi_4_17.civicrm_address.state_province_id
FROM abc_abc_drupal_civi_4_17.civicrm_address,abc_drupal.civicrm_address
WHERE abc_drupal.civicrm_address.state_province_id IS NULL
AND abc_abc_drupal_civi_4_17.civicrm_address.state_province_id IS NOT NULL
AND abc_abc_drupal_civi_4_17.civicrm_address.id = abc_drupal.civicrm_address.id
AND abc_abc_drupal_civi_4_17.civicrm_address.contact_id IS NOT NULL;
MySQL puts JOIN in the UPDATE clause, not through a separate FROM (the FROM is used by SQL Server and Postgres).
Your query as written is hard to decipher. I would strongly recommend that you use table aliases, so the query is more easily written and read:
UPDATE abc_drupal.civicrm_address a JOIN
abc_abc_drupal_civi_4_17.civicrm_address aa
ON aa.id = a.id
SET a.state_province_id = aa.state_province_id
WHERE a.state_province_id IS NULL
aa.state_province_id IS NOT NULL AND
aa.contact_id IS NOT NULL;
You need to specify set later like below:
UPDATE TABLEA a
JOIN TABLEB b ON a.join_colA = b.join_colB
SET a.columnToUpdate = [something]
UPDATE abc_drupal.civicrm_address
join abc_abc_drupal_civi_4_17.civicrm_address inner join abc_drupal.civicrm_address
on abc_abc_drupal_civi_4_17.civicrm_address.id = abc_drupal.civicrm_address.id
SET abc_drupal.civicrm_address.state_province_id = abc_abc_drupal_civi_4_17.civicrm_address.state_province_id
where abc_drupal.civicrm_address.state_province_id IS NULL
AND abc_abc_drupal_civi_4_17.civicrm_address.state_province_id IS NOT NULL
AND abc_abc_drupal_civi_4_17.civicrm_address.contact_id IS NOT NULL
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;
I'm trying to do a fairly simple update of one column from one table to the same column in another.
I have two tables:
container (id, barcode_1, location)
test(id, barcode_1, location)
Both tables are exactly the same except for the barcode_1 column. I want to update container.barcode_1 with the values from test.barcode_1 on all matching ids.
This is the code I'm using:
update container
set container.barcode_1 = test.barcode_1
FROM container INNER JOIN test ON container.id = test.id;
When I run that code, I get this error:
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 container INNER JOIN test ON container.id = test.id' at line 3
Any ideas one why this might be failing?
Your UPDATE syntax is wrong here. It should be
update container
INNER JOIN test ON container.id = test.id
set container.barcode_1 = test.barcode_1;
(OR) Using table alias
update container c
INNER JOIN test t ON c.id = t.id
set c.barcode_1 = t.barcode_1;
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';