Anyone who can help me with the following query: I have database with the following tables: Emails ======== ID int autoincrement Address varchar(255) Status enum Sent ======== ID int autoincrement EmailID int (FK to Emails.ID) SentDate date Status enum (1 = accept , 2 = deferred, 3 = bounced) Headers ======== ID int autoincrement EmailID int (FK to Emails.ID) Header varchar(255) Value text How to write SQL statement to update Emails.Status to 11 for all those addresses who have had three or more emails bounced back in the last week and who's subject was "Bounce Test".
I tried writing it :UPDATE EmailsSET status = 11FROM Emails E, Headers HWHERE H.Header = “Bounce Testâ€AND H.EmailID = E.IDAND E.ID IN ( SELECT EmailID FROM Sent WHERE SentDate >= DATEADD(DD,-7, GETDATE())GROUP BY IDHAVING COUNT(*) >=3) But seems like Update doesnt have FROM syntax.Need to fix this?
Dear Question Asker; First: there is no such thing as "Emails.Status" what you do is you have to ensure the database is chosen, ( if you are using php: mysql_select_db('db'); and then you do this: $query = "update Emails set Status='11' where ... " if you are trying to set the status to '11' based on a condition from another table, then you have 2 choices maybe more. in PHP you can find the e-mail id from the table then take that id to the query for the 'Emails' table. or you can merge them together in 1 query and then update whatever is needed. once you merge 2 tables via mysql query.. you should specify which rows you want to merge perhaps, if the rows are unique then you can merge everything and decide how to modify things. please use search engines on the internet ( planet earth: year 2012 ) and look for keywords such as " mysql merge tables " merging tables makes MYSQL struggle a little bit, however if it is a necessity, then it is a necessity - Sumer Kolcak