Jump to content

[SOLVED] Sql WHERE over multiple fields?


Mutley

Recommended Posts

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

 

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. :)

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]

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.