gple Posted September 10, 2007 Share Posted September 10, 2007 I have two tables (stats, years) stats has two categories (ID, TID, text) year has two categories (Year, TID) If I want to update the text by in STATS by year, does this look right. mysql_query("UPDATE stats, year SET text="hello" where year.tid=stats.tid and year.year='2007'"); Quote Link to comment https://forums.phpfreaks.com/topic/68732-update-in-php/ Share on other sites More sharing options...
lemmin Posted September 10, 2007 Share Posted September 10, 2007 No, you can't have the criteria for updating one table be dependant soley on another. The criteria has to be checking a field in the table you are updating. For example: "UPDATE stats year SET text='hello' WHERE stats.tid = (SELECT tid FROM year WHERE year = 2007 LIMIT 1)"; You only don't need (single) quotes for numerical date. I assume 2007 is in your database as a number, if it is a string it DOES need the quotes. This query updates the field 'text' where the id is the same id as the one in the year table that has a year of 2007. Of course, this has to be limited to one to do it this way. If you wanted to update ALL text fields that matched that year, you would need to use a join, or something. Quote Link to comment https://forums.phpfreaks.com/topic/68732-update-in-php/#findComment-345520 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.