What's wrong with my UPDATE statement? - mysql

I'm learning SQL, and I'm trying to import data from one table to another. I've been having some success, but the update statement is giving me issues. I have a batting average column in the new table, and I'm trying to get the actual values into it.
ie. hits (h) and at bats (ab) are in table 1 but I want h/ab = avg in table 2.
I tried:
UPDATE seasonbatting s
Set OBP = (H/AB)
FROM Batting b
WHERE b.playerID = s.playerID
AND b.yearID = s.yearID;
but I get an 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 batting b
WHERE s.playerID = b.playerID AND s.yearID = p.yearID' at line 3"

MySQL does not support a FROM clause in the UPDATE. Your syntax looks more like Postgres or SQL Server.
You want:
UPDATE seasonbatting s JOIN
Batting b
ON b.playerID = s.playerID AND b.yearID = s.yearID
Set s.OBP = (H/AB)

Related

MySQL syntax error in my code that I can't find manually

Joining two tables to get correct information and update one value from one table by conditioning with some specific conditions
SQL say that it has syntax error at line 5 although it didn't fix the syntax error automatically if he (SQL) knew that
UPDATE
sale
SET
amount = 10000
FROM
sale
JOIN delivery ON delivery.sale_id = sale.id
WHERE
sale.`status` = "active"
AND delivery.services_id = 7;
You have an error in your SQL syntax; it seems the error is around:
'FROM sale JOIN delivery ON delivery.sale_id = sale.id WHERE
sale.status ' at line 5
The correct syntax for a MySQL multi-table update (manual) puts the table references at the beginning of the query:
UPDATE
sale
JOIN delivery ON delivery.sale_id = sale.id
SET
amount = 10000
WHERE
sale.`status` = "active"
AND delivery.services_id = 7;
Demo on dbfiddle
MySQL doesn't support UPDATE ... FROM (that is a SQL Server feature)
Here is the reference to mySQL update syntax:
https://dev.mysql.com/doc/refman/8.0/en/update.html

Syntax error while trying to run a update query in mysql

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

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.

SQL syntax error with UPDATE ... FROM ... JOIN

I'm learning SQL and have built up a little database so far. Now I have 2 tables called countries_visited and regions_visited with the following schema,
countries_visited: [id,countries,year_visited]
regions_visited: [r_id,regions,c_id,month_visited]
The id column in countries_visited is a primary key, and the c_id column of regions_visited is a foreign key linked to countries_visited
The month_visited column in regions_visited is newly created and all entries are now NULL. What I want to do is to individually insert the entry into this new column with conditions.
So, I tried the following statement taking from the earlier examples here in StackOverflow,
UPDATE regions_visited
SET month_visited = 9
FROM countries_visited
JOIN regions_visited ON countries_visited.id = regions_visited.c_id
WHERE regions_visited.regions = 'Seoul'
AND countries_visited.year_visited = 2006
AND countries_visited.countries = 'South Korea';
By running this command on MySQL server, it shows this error:
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 regions_visited INNER JOIN countries_visited
ON regions_visited.c_id = coun' at line 1
However, if neglecting the UPDATE and SET part of the statement, mysql shows the joined table with only 1 row that I would like to update. What did I do wrong? How to make this work?
Thank you in advance!
In MySQL, the join clauses come before the setclause:
UPDATE regions_visited rv
JOIN countries_visited cv ON ON cv.id = rv.c_id
SET month_visited = 9
WHERE rv.regions = 'Seoul' AND
cv.year_visited = 2006 AND
cv.countries = 'South Korea';
Move the Join before SET. Try this
UPDATE regions_visited
JOIN regions_visited
ON countries_visited.id = regions_visited.c_id
SET month_visited = 9
WHERE regions_visited.regions = 'Seoul'
AND countries_visited.year_visited = 2006
AND countries_visited.countries = 'South Korea';

Update a column in a database with values from another colum of another database

I need to update a column in a database with values from another column of another database.
Here is my query:
UPDATE dbA.tableA as a
SET a.columnA = b.columnB
FROM dbB.tableB as B
WHERE
a.num = b.num
And I get 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 'FROM ...
Both databases are in the same server.
How can I solve this?
The correct format of update with join is
UPDATE dbA.tableA a
join dbB.tableB b on a.num = b.num
SET a.columnA = b.columnB