How I skip one cell in MySQL query

16
April 16, 2019, at 02:20 AM

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!

Answer 1

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

READ ALSO
need help for query code export from 2table (by php) in mysql

need help for query code export from 2table (by php) in mysql

i need code for export favs in tablei make 2 table in mysql one : tbl_post two : tbl_markshow

32
Implementing Auto-Bidding Functionality with JSP/MySQL

Implementing Auto-Bidding Functionality with JSP/MySQL

Currently working on a project and could use some advice for how to approach implementing an Auto-Bidding system for a web application similar to Ebay

24
Calculate duration for bookings in a period between two time slots

Calculate duration for bookings in a period between two time slots

In a booking system I want to calculate the sum of hours a resource has been booked between two dates (2019-01-01 to 2019-01-02) from 8am to 4pm

19
MYSQL: Copy all data and structure if not exist

MYSQL: Copy all data and structure if not exist

i have two database new and old database

25