NevadaSam Posted September 29, 2006 Share Posted September 29, 2006 I know to change the data in a I simply send an update querry:[quote]UPDATE foodsSET SameColumn = 'Red Peas'WHERE SameColumn = 'rpeas'[/quote]But I have several more categories in that SameColumn that I also need to change.[quote]SET SameColumn = 'Whole Milk'WHERE SameColumn = 'wmilk'SET SameColumn = 'T-Bone Steak'WHERE SameColumn = 'tsteak'SET SameColumn = 'Tatar Tots'WHERE SameColumn = 'ttots'[/quote]Is there a way I can send them all at once or will each have to be done one at a time?Sam Quote Link to comment Share on other sites More sharing options...
fenway Posted September 29, 2006 Share Posted September 29, 2006 You can use a CASE statement. Quote Link to comment Share on other sites More sharing options...
NevadaSam Posted September 29, 2006 Author Share Posted September 29, 2006 The case statement would have to be ran from within a php script right? I am new at this but I think I can figure that out. Thanks! I was hoping that it would be something that could work from a query by adding ANDs or something. I'll let you know how I do tomorrow.Sam Quote Link to comment Share on other sites More sharing options...
fenway Posted September 29, 2006 Share Posted September 29, 2006 No, MySQL has a CASE statment. Quote Link to comment Share on other sites More sharing options...
NevadaSam Posted September 29, 2006 Author Share Posted September 29, 2006 Thanks for telling me about the CASE statement, fenway. This works. The first set of WHEN - THEN statements does the trick.[code]UPDATE thisTableSET thisColumn = CASE thisColumnWHEN 'd1' THEN 'Data One'WHEN 'd2' THEN 'Data Two'WHEN 'd3' THEN 'Data Three'WHEN 'd4' THEN 'Data Four'WHEN 'Data One' THEN 'Data One'WHEN 'Data Two' THEN 'Data Two'WHEN 'Data Three' THEN 'Data Three'WHEN 'Data Four' THEN 'Data Four'END[/code]I had to add the second set of WHEN - THEN statements because if the query was ran after the new values were set the field would be set as NULL if the old WHEN value was not found.Is there a way to express an ELSE statement so I would not have to use the second set of WHEN - THEN statements?Sam Quote Link to comment Share on other sites More sharing options...
fenway Posted September 30, 2006 Share Posted September 30, 2006 There is an ELSE:[code]UPDATE thisTableSET thisColumn = CASE thisColumnWHEN 'd1' THEN 'Data One'WHEN 'd2' THEN 'Data Two'WHEN 'd3' THEN 'Data Three'WHEN 'd4' THEN 'Data Four'ELSE thisColumnEND[/code] Quote Link to comment Share on other sites More sharing options...
NevadaSam Posted September 30, 2006 Author Share Posted September 30, 2006 Thanks, I knew the ELSE statement could be used. I just was not sure what to say with it. Quote Link to comment 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.