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
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.
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.
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';
I try to execute the query but get an error. This is my query:
UPDATE
prepares_for_exam
SET
prepares_for_exam.exam_id = product.id
FROM
prepares_for_exam,
product
WHERE
prepares_for_exam.id = product.prepares_for_exam_id
and I get the 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 prepares_for_exam, product WHERE prepares_for_exam.id = product.prepares_fo' at line 1
I did the Update Query 100-times with an FROM clause and never had problems... Whats my fault?!?
You are using the SQL-Server syntax. In MySQL it is a little bit different.
UPDATE
prepares_for_exam
JOIN
product
ON
prepares_for_exam.id = product.prepares_for_exam_id
SET
prepares_for_exam.exam_id = product.id
UPDATE
prepares_for_exam
SET
prepares_for_exam.exam_id = (select product.id FROM
prepares_for_exam JOIN product on prepares_for_exam.id = product.prepares_for_exam_id)
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