tags:

views:

218

answers:

3

I have the following query

SELECT e.topicShortName, d.catalogFileID, e.topicID
FROM catalog_topics a
LEFT JOIN catalog_files_join b ON a.catalogID = b.foreignKey
LEFT JOIN catalog_files_join c ON c.foreignKey = b.catalogFileID
LEFT JOIN catalog_files d ON d.catalogFileID = b.catalogFileID
LEFT JOIN catalog_lu_topics e ON a.topicID = e.topicID
WHERE b.fileTypeID = 'gvl401'
AND c.fileTypeID = 'gvl25'
AND e.parentID = 'top305'
AND a.sortorder =1
AND e.topicID = 'top318'

which fetches me one row of data:

topicShortName  catalogFileID  topicID
 Welcoming       cfil960       top318

I want to run a update statement so that I can update catalogFileID to 'cfil123'. I have the topicID with me, it is 'top318'

catalogFileID belongs to catalog_files

I cant seem to wrap my head around the update statement which will achieve this..

I do not mind doing multiple updates. But after the update statements, the above select query should return cfil123. But I cant just update all the tables where catalogFileID is used..

CORRECT ANSWER:

UPDATE catalog_topics a
LEFT JOIN catalog_files_join b ON a.catalogID = b.foreignKey
LEFT JOIN catalog_files_join c ON c.foreignKey = b.catalogFileID
LEFT JOIN catalog_files d ON d.catalogFileID = b.catalogFileID
LEFT JOIN catalog_lu_topics e ON a.topicID = e.topicID
SET d.catalogFileID = 'Cfil123',
    b.catalogFileID = 'Cfil123',
    c.foreignKey = 'Cfil123'
WHERE b.fileTypeID = 'gvl401'
AND c.fileTypeID = 'gvl25'
AND e.parentID = 'top305'
AND a.sortorder =1
AND e.topicID = 'top318'
A: 

Better to begin a transaction and update each and every table separately. SQL update statements are meant to affect one table per statement.

David Andres
A: 
  1. Identify all the tables that have this ID as a foreign key

  2. Wrap your code in a transaction

  3. List out individual updates. I can't tell exactly what your schema is, but I think it's:

    UPDATE category_files set catalogFileID='cfil123' where categoryFileID='cfil960'; UPDATE catalog_files_join set catalogFileID='cfil123' where categoryFileID='cfil960' etc.

This would update all references to the category file, which may not be what you want.

ndp
A: 

Simply replace 'SELECT...FROM' with 'UPDATE' and add a 'SET ...' clause before WHERE:

UPDATE catalog_topics a
LEFT JOIN catalog_files_join b ON a.catalogID = b.foreignKey
LEFT JOIN catalog_files_join c ON c.foreignKey = b.catalogFileID
LEFT JOIN catalog_files d ON d.catalogFileID = b.catalogFileID
LEFT JOIN catalog_lu_topics e ON a.topicID = e.topicID
SET d.catalogFileID = 'cfil123',
    b.catalogFileID = 'cfil123'
WHERE b.fileTypeID = 'gvl401'
AND c.fileTypeID = 'gvl25'
AND e.parentID = 'top305'
AND a.sortorder =1
AND e.topicID = 'top318'

Make sure you specify which tables' fields to update in the SET clause using <table>.<field> notation.

Edit: Removed extra comma ...

too much php
I had tried exacly that, but did not update b.catalogFileID. Hopefully your answer works but it your query is giving me this error ( I am using MySQL): #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 'WHERE b.fileTypeID = 'gvl401'AND c.fileTypeID = 'gvl25'AND e.parentID = 'top' at line 8
oh nvmd you had extra comma
damnit. now it says zero rows updated :(
If it says 'zero rows updated', then change it back to a select to see what will be updated - change 'UPDATE' to 'SELECT * FROM' and get rid of the 'SET ....' clause. This shows you exactly which rows will be updated. Also, your query uses LEFT JOINs, it is possible that no match is found in tables 'b' or 'd', so nothing needs to be updated.
too much php
your answer guided me towards the correct answer. I will accept your answer and post the correct answer as well in my post.