SQL Update after Joining Two Tables -Not working - mysql

I have a update query
update B set B.i_description='travncore testing',B.Tm_id=35
from backlog B join backToSprint B1 on
B.b_id=B1.fk_back_id where B1.s_id=18
when run this query i got an error like
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 backlog B inner join backToSprint B1 on
B.b_id=B1.fk_back_id where B2.s_id=' at line 1
Any help would be greatly appreciated .

You can try below set should be afer join and before where clause
update backlog B
join backToSprint B1 on B.b_id=B1.fk_back_id
set B.i_description='travncore testing',B.Tm_id=35
where B1.s_id=18

The right grammer can be found at mysql-update-a-joined-table,so you can try with below
update backlog B
join backToSprint B1 on B.b_id=B1.fk_back_id
set B.i_description='travncore testing',B.Tm_id=35
where B1.s_id=18

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.

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.

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

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'