Mutley Posted July 24, 2007 Share Posted July 24, 2007 Is it possible to do a WHERE statement over several fields, so... field1 = 3 field2 = 4 field6 = 9 field0 = 1 Then... $sql = "UPDATE table SET "the field" = '00' WHERE "the fields" = '4'; Result... field1 = 3 field2 = 00 field6 = 9 field0 = 1 Thanks. Link to comment https://forums.phpfreaks.com/topic/61557-solved-sql-where-over-multiple-fields/ Share on other sites More sharing options...
nicangeli Posted July 24, 2007 Share Posted July 24, 2007 N00b Here, Why not just runa query irst to determine which of the fields contain 4 ?? Link to comment https://forums.phpfreaks.com/topic/61557-solved-sql-where-over-multiple-fields/#findComment-306357 Share on other sites More sharing options...
pocobueno1388 Posted July 24, 2007 Share Posted July 24, 2007 Yes it is. Are the "fields" from the database, or a form? Link to comment https://forums.phpfreaks.com/topic/61557-solved-sql-where-over-multiple-fields/#findComment-306360 Share on other sites More sharing options...
Mutley Posted July 24, 2007 Author Share Posted July 24, 2007 It's a database. I have a terrible feeling this is going to be a really easy/obvious solution. Link to comment https://forums.phpfreaks.com/topic/61557-solved-sql-where-over-multiple-fields/#findComment-306366 Share on other sites More sharing options...
Mutley Posted July 24, 2007 Author Share Posted July 24, 2007 Would I do it using LIKE or AND etc? Link to comment https://forums.phpfreaks.com/topic/61557-solved-sql-where-over-multiple-fields/#findComment-306536 Share on other sites More sharing options...
Barand Posted July 24, 2007 Share Posted July 24, 2007 If those are int fields it may not accept '00', only '0'. But, if they are varchar, UPDATE mutley SET field0 = IF(field0='4', '00', field0), field1 = IF(field1='4', '00', field1), field2 = IF(field2='4', '00', field2), field6 = IF(field6='4', '00', field6) This will change the values in every row unless you specify a WHERE clause to state which rows to update Link to comment https://forums.phpfreaks.com/topic/61557-solved-sql-where-over-multiple-fields/#findComment-306603 Share on other sites More sharing options...
Mutley Posted July 24, 2007 Author Share Posted July 24, 2007 How would I do a where clause? They are INT, the 00 was just an example to stand out. I want to update it like this: $sql = "UPDATE table SET "the field" = '999' WHERE "the fields" = '4'; So it has to find the "4" in the 4 different fields (only 1 field will contain it, it's unique, like a user_id) then change it. Hope that makes sense. Link to comment https://forums.phpfreaks.com/topic/61557-solved-sql-where-over-multiple-fields/#findComment-306608 Share on other sites More sharing options...
GingerRobot Posted July 24, 2007 Share Posted July 24, 2007 I might be missing the point completely, but dont you just list the fields and use or? $sql = "UPDATE table SET the field = 999 WHERE field1=4 OR field2=4 OR field3=4 OR field4=4"; Link to comment https://forums.phpfreaks.com/topic/61557-solved-sql-where-over-multiple-fields/#findComment-306613 Share on other sites More sharing options...
Barand Posted July 24, 2007 Share Posted July 24, 2007 if you have [pre] +----+--------+--------+--------+--------+ | id | field0 | field1 | field2 | field6 | +----+--------+--------+--------+--------+ | 1 | 4 | 3 | 5 | 6 | | 2 | 3 | 4 | 5 | 6 | | 3 | 1 | 3 | 4 | 7 | | 4 | 1 | 2 | 0 | 4 | +----+--------+--------+--------+--------+ [/pre] and you only want to update the row where the id is 3 UPDATE mutley SET field0 = IF(field0=4, 999, field0), field1 = IF(field1=4, 999, field1), field2 = IF(field2=4, 999, field2), field6 = IF(field6=4, 999, field6) WHERE id = 3 --> [pre] +----+--------+--------+--------+--------+ | id | field0 | field1 | field2 | field6 | +----+--------+--------+--------+--------+ | 1 | 4 | 3 | 5 | 6 | | 2 | 3 | 4 | 5 | 6 | | 3 | 1 | 3 | 999 | 7 | | 4 | 1 | 2 | 0 | 4 | +----+--------+--------+--------+--------+ [/pre] Link to comment https://forums.phpfreaks.com/topic/61557-solved-sql-where-over-multiple-fields/#findComment-306628 Share on other sites More sharing options...
Mutley Posted July 24, 2007 Author Share Posted July 24, 2007 Awesome, thanks! Link to comment https://forums.phpfreaks.com/topic/61557-solved-sql-where-over-multiple-fields/#findComment-306659 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.