Jump to content

[SOLVED] Sql WHERE over multiple fields?


Mutley

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.