Using MySQL: Update field with values using Inner Join - mysql

I have a main table and an index table. Both tables share a common primary field called "L_Status". I want to update the data in the main table from "L_Status" values (integers) into "L_StatusLV" (readable text values) based on the reference in the index table called "status". Here is the code I've entered into PHPmyAdmin to accomplish this:
UPDATE markers.L_Status
FROM markers
INNER JOIN STATUS ON markers.L_Status = status.L_Status
WHERE markers.L_Status = status.L_StatusLV
PHPmyAdmin returns the following 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 markers INNER JOIN STATUS ON markers.L_Status = status.L_Status WHERE ma' at line 2
Any advice on the sytax error here?

MySQL's UPDATE JOIN syntax differs from SQL Server's (which looks like what you have):
UPDATE
/* First join the tables */
markers
INNER JOIN status ON markers.L_Status = status.L_Status
/* Then specify the new value in the SET clause */
SET markers.L_Status = status.L_StatusLV
However, as you noted above the current values are integers. If the column markers.L_Status is an INT column rather than a CHAR/VARCHAR as I assume the human-readable column is, this won't work.
Visit MySQL's UPDATE syntax reference for the full syntactic details. In particular, the table_references.

Related

Syntax error in sql query of copying data from one table to another

I have the following query :
update event
set event.Paid = payment.amount
from event, payment
where payment.event_id = event.eid
The above query gives following 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 event, payment where payment.event_id = event.eid' at line
3
You join clause is wrong anyway
You should not use the implicit join syntax based on comma separated table name and where
you should use explicit join syntax (for mysql)
update event
INNER JOIN payment ON payment.event_id = event.eid
set event.Paid = payment.amount

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

insert count from another table onto master table

I've got one master table and several additional tables. Each row is a individual person's record.
I want to insert an individual's counts from the additional table as new columns onto the master table. There is a unique ID that can link the two.
I found these instructions: MySQL: UPDATE table with COUNT from another table?
My code is:
SELECT * FROM leeds.salesforce_contacts as allmemcomb
LEFT OUTER JOIN leeds.leenk_ladder_history as ladhist on allmemcomb.salesforce_id = ladhist.member_id
LEFT OUTER JOIN leeds_so.leenk_ladder_config as ladconf on ladhist.member_id = ladconf.ladder_config_id
UPDATE allmemb
set count = (
select count (ladder_change)
from ladhist where ladhist.member_id = allmembcomb.salesforce_id
);
But I'm getting the following 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 'UPDATE allmemb set count = ( select count (ladder_change) from ladhist wher' at line 13
thoughts?
You are missing the semicolon at the end of the statement before UPDATE.

MySql subquery error with delete statement

I'm learning sql from these wikibooks pages and I'm trying to answer the last question "Remove all boxes from saturated warehouses" using mysql syntax. The following is what I came up with on my own mainly using previous answers from the same page.
delete from Boxes as b
where b.Warehouse in (
select w.Code
from ( select *
from Warehouses) as w
where w.Capacity < ( select count(*)
from Boxes bb inner join Warehouses ww
on bb.Warehouse = ww.Code
group by bb.Contents) ) ;
The query for this question on the site is generating
this solution doesn't work with mysql 5.0.67
ERROR 1093 (HY000): You can't specify target table 'Boxes' for update in FROM clause
That's why I'm using multiple sub queries as a way to come around this mysql message. However, I have an error in my query and therefore it's not running.
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 'as b where b.Warehouse in ( select w.Code
from ( sel' at line 1
Any idea?
I assume Boxes and Warehouse are tables, you gave Boxes an alias and used it on the Warehouse table, that's probably why it didn't work. Maybe you should try with a query after 'delete from'.
I think your warehouses table returns from than one columns (since * is used). Replace * with the column u want to get the output from.