Error with my UPDATE mysql query, with a lot of join - mysql

When I try the following query :
UPDATE cache_implementation
SET parent_through_compared_id = ncp.nid, parent_through_feature_id = nfp.nid
FROM cache_implementation n
INNER JOIN cache_compare nc ON n.compared_id = nc.nid
INNER JOIN cache_implementation ncp ON (nc.nid = ncp.compared_id AND n.feature_id = ncp.feature_id)
INNER JOIN cache_feature nf ON n.feature_id = nf.nid
INNER JOIN cache_implementation nfp ON (nf.nid = nfp.feature_id AND n.compared_id = nfp.compared_id)
I have the following 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 cache_implementation n INNER JOIN cache_compare nc ON n.compared_id = nc' at line 3
Through this query, I try to update two fields with value located in some other table, by making a mass update query.

You are using used in TSQL. Here's for MySQL.
UPDATE cache_implementation n
INNER JOIN cache_compare nc
ON n.compared_id = nc.nid
INNER JOIN cache_implementation ncp
ON (nc.nid = ncp.compared_id AND n.feature_id = ncp.feature_id)
INNER JOIN cache_feature nf
ON n.feature_id = nf.nid
INNER JOIN cache_implementation nfp
ON (nf.nid = nfp.feature_id AND n.compared_id = nfp.compared_id)
SET parent_through_compared_id = ncp.nid, parent_through_feature_id = nfp.nid

In MySQL multi-table update statement, the SET clause follows the table references. (This differs from the syntax used in other databases.)
To fix your statement, delete that first line, move the line with SET to the bottom, qualify the column references with the table alias, and change FROM to UPDATE. Voila.
UPDATEcache_implementation
FROM cache_implementation n
INNER JOIN ...
INNER JOIN ...
SET n.col = expr, n.col2 = expr
Multi-table update syntax documented here: http://dev.mysql.com/doc/refman/5.5/en/update.html

Related

Not unique table/alias on join

I intend to do an update based on join, but getting an error. What is missing?
update
vna.patients,
vna.patient_observations,
vna.studies,
vna.series,
vna.instances,
vna.sop_classes,
vna.files,
vna.modalities,
vna.issuers
join patient_observations on atients.patient_id=patient_observations.patient_id
join studies
on patient_observations.study_id=studies.study_id
AND studies.patient_id=patients.patient_id
join series
on series.study_id=studies.study_id
join instances
on instances.series_id=series.series_id
join sop_classes
on sop_classes.sop_class_id=instances.sop_class_id
join files
on files.instance_id=instances.instance_id
left join modalities
on modalities.modality_id=series.modality_id
left join issuers
on (patients.issuer_of_patient_identifier=issuers.issuer_id)
set PATIENT_NAME='AAPM'
WHERE PATIENT_IDENTIFIER='TG18-2002';
ERROR 1066 (42000): Not unique table/alias: 'patient_observations'
You don't need to specify table names again which appears in join part
UPDATE
vna.patients
JOIN patient_observations
ON patients.patient_id = patient_observations.patient_id
JOIN studies
ON patient_observations.study_id = studies.study_id
AND studies.patient_id = patients.patient_id
JOIN series
ON series.study_id = studies.study_id
JOIN instances
ON instances.series_id = series.series_id
JOIN sop_classes
ON sop_classes.sop_class_id = instances.sop_class_id
JOIN files
ON files.instance_id = instances.instance_id
LEFT JOIN modalities
ON modalities.modality_id = series.modality_id
LEFT JOIN issuers
ON (patients.issuer_of_patient_identifier = issuers.issuer_id)
SET PATIENT_NAME = 'AAPM'
WHERE PATIENT_IDENTIFIER = 'TG18-2002' ;
Why are you mixing the two different JOIN syntaxes? Simple rule: Never use commas in the FROM clause (and that goes for UPDATE as well). I think you intend:
update vna.patients p
patient_observations po
on p.patient_id = po.patient_id join
studies st
on po.study_id = st.study_id AND
st.patient_id = p.patient_id join
series s
on s.study_id = st.study_id join
instances i
on i.series_id = s.series_id join
sop_classes sc
on sc.sop_class_id = i.sop_class_id join
files f
on f.instance_id = i.instance_id left join
modalities m
on m.modality_id = s.modality_id left join
issuers iss
on (p.issuer_of_patient_identifier = iss.issuer_id)
set p.PATIENT_NAME = 'AAPM'
where p.PATIENT_IDENTIFIER = 'TG18-2002';
That seems way too complicated. I'm guessing you just want:
update vna.patients p
set p.PATIENT_NAME = 'AAPM'
where p.PATIENT_IDENTIFIER = 'TG18-2002';
The error message is very clear, the table patient_observations is listed twice in the table references in your query. Give the second one a different alias if you really need to join it again in the same query:
...
vna.issuers
join patient_observations as po2 on patients. ....
Otherwise, remove one of them.
Also try to use the ANSI SQL join syntax instead of this old syntax.

MySQL query to SQL Server query

I defined sql query and it runs with no problem on MySQL (I am using MySQL) , but when I am trying to execute it on client site (they uses SQL Server)
I am getting "Error: Incorrect syntax near 'si'." error message
Hope someone can help me to define right syntax.
The query is following:
update stepinstance si
inner join cesteplink l on si.id = l.stepinstance_id
inner join prompt p on si.prompt_id = p.id
set si.principal_id = 29160180
where l.case_id = 29179541
and si.principal_id = 1799409
and si.status = 'In Progress'
set has to be before the join conditions.
update si
set si.principal_id = 29160180
from stepinstance si
inner join cesteplink l on si.id = l.stepinstance_id
inner join prompt p on si.prompt_id = p.id
where l.case_id = 29179541
and si.principal_id = 1799409
and si.status = 'In Progress'

How can I update filed value from a select with inner join?

I am trying to update a field value in a mysql database using a select query with inner join
I currently get
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 cc WHERE cc.account_id = na.account_id' at line 5
UPDATE accounts AS na
SET na.pdm_id = (
SELECT cp.person_id FROM `temp_accounts` AS ta INNER JOIN call_managment_system.accounts AS a ON ta.company_code = a.company_code
INNER JOIN contact_personal AS cp ON cp.name = ta.FSM AND contact_link = 'PDM'
)
WHERE a.account_id = na.account_id
How can I fix this query to work? I want to update the field called pdm_id to set it equal to cp.person_id
Thanks
UPDATE accounts na
INNER JOIN call_managment_system.accounts a
ON a.account_id = na.account_id
INNER JOIN temp_accounts ta
ON ta.company_code = a.company_code
INNER JOIN contact_personal cp
ON cp.name = ta.FSM
SET na.pdm_id = cp.person_id
WHERE contact_link = 'PDM'

Unknown Column id in MySQL Query

Can someone help me I've got 2 MySQL queries that get unknown column id when I try to run them. I might add that I am converting this database from SQLServer 2005 to MySQL and they run fine in SQL Server 2005.
Here's 1 of them:
SELECT DISTINCT g.id AS `genre`
FROM media_playlist_sequence MPS
INNER JOIN media M ON M.`key` = MPS.media_key
INNER JOIN media_playlists MP ON MP.`key` = MPS.playlist_key
INNER JOIN node_media_playlist NMP ON NMP.playlist_key = MP.`key`
INNER JOIN nodes N ON N.`key` = NMP.node_key
INNER JOIN media_files MF ON MF.media_key = M.`key`
INNER JOIN media_locations ML ON ML.media_file_key = MF.media_file_key
AND ML.node_key = n.`key`
INNER JOIN media_genres MG ON MG.media_key = M.`key`
INNER JOIN genres G ON G.`key` = MG.genre_key
WHERE M.is_ready = 1
AND MP.id = 'Channels'
AND N.id = 'VIC-WIN7'
AND mf.is_quad_image = 0
My guess is that it's a case sensitivity issue. MySQL can be case sensitive by default whereas SQL Server is not.

Syntax error Mysql

No Idea why this is not working - could someone please help?
update c set c.images = ""
from j17_content c
inner join j17_jreviews_content rc on rc.contentid = c.id
inner join j17_categories cat on cat.id = c.catid
where cat.path like "cakes%"
and c.created_by in (62,63,99)
and rc.email = 'email'
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 j17_content c inner join j17_jreviews_content rc on rc.contentid = c.id in' at line 2
UPDATE:
Now trying
UPDATE j17_content c SET c.images=''
inner join j17_jreviews_content rc on rc.contentid = c.id
inner join j17_categories cat on cat.id = c.catid
where cat.path like 'cakes%'
and c.created_by in (62,63,99)
and rc.email = 'email'
still getting
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 'inner join j17_jreviews_content rc on rc.contentid = c.id inner join j17_catego' at line 2
UPDATE doesn't take a FROM.
The syntax should be UPDATE j17_content c SET c.images="" INNER JOIN ...
This is essentailly your code.
Try it this way:
update j17_content c
inner join j17_jreviews_content rc on rc.contentid = c.id
inner join j17_categories cat on cat.id = c.catid
where cat.path like 'cakes%'
and c.created_by in (62,63,99)
and rc.email = 'email'
set c.images = '';
This is an UPDATE JOIN
I have a refactored version of the query in mind
update
(
SELECT content_id id
FROM j17_jreviews_content
WHERE email = 'email'
) rc
inner join j17_content c USING (id)
inner join
(
SELECT id catid
FROM j17_categories
WHERE created_by in (62,63,99)
AND path like 'cakes%'
) cat USING (catid)
set c.images = '';
You will need indexes for the subqueries involved
ALTER TABLE j17_categories ADD INDEX created_by_path_id_ndx (created_by,path,id);
ALTER TABLE j17_jreviews_content ADD INDEX email_content_id_ndx (email,content_id);
replace your double quotes(") with single ones(')
Sorry this is not really the way you guys suggested - but I got a list of IDs and updated them using no joins with an in statement of all the IDs