jason5137715
jason5137715

Reputation: 117

How I skip one cell in MySQL query

I'm trying to make PHP code update column called "downloads" to zero inside MySQL.

After the command executed I want to update one cell under the column downloads to "100" for a user called "guest" because the first query reset it to zero. This is my code:

<?php
require_once 'connect.php';
if( $link->query("update users set downloads = 0") ){
    // succesful
} else {
    // fail
}
?>

My table structure like this:

| NAME | PASSWORD | DOWNLOADS 
| Johny | XieofEnfoEQ | NULL 
| guest | nfOEnfoiJEJj | 100

Is there any way to make the query update all downloads but keep one cell for the user guest without any changing!

Upvotes: 0

Views: 21

Answers (1)

Nick
Nick

Reputation: 147146

You can adjust your query to set the downloads value to 100 for guest and 0 for all others using a CASE expression:

UPDATE users
SET downloads = CASE WHEN `NAME` = 'guest' THEN 100 ELSE 0 END

Demo on dbfiddle

Upvotes: 2

Related Questions