Help with construction of one update MySQL query
#1

Hey, I have two columns and I want always update only one of them (in case it is 0) so for example, when column A equals to zero, my value will be set only to column A else keep value at column A and update column B

UPDATE acc_mobile_messages (case when archived = 0 then SET archived = %i else SET archivedex = %i end

I've been trying something like this, but i'm sure that it's wrong, thanks.
Reply
#2

Like this for example ? Because, I can't really see what do you exactly mean

PHP код:
forward UpdatePlayerColumnInt(SqlIDColumnName[], ValInt);
public 
UpdatePlayerColumnInt(SqlIDColumnName[], ValInt)
{
    
format(sqlsizeof(sql), "UPDATE acc_mobile_messages SET %s = %d WHERE IDMYSQL = %d LIMIT 1"ColumnNameValIntSqlID);
    return 
1;
}
SqlIDID of player into your MYSQL.
ColumnNameSpecify the column that you wish update it.
ValIntThe value that you want to set it
Reply
#3

Nah, I all want is update only one column of row (of two), which is equal to 0 (non set)
Reply
#4

I think that this should work if I understand the problem correctly:

Код:
UPDATE `acc_mobile_messages`
SET 
`archivedex` = (CASE `archived` WHEN 1 THEN %i ELSE `archivedex` END),
`archived` = (CASE `archived` WHEN 0 THEN %i ELSE `archived` END)
WHERE `databaseId` = %i;
We try to set `archivedex` first depending on the value of `archived` -- only if `archived` is 1 then it will update. Then we try to set `archived` to 1 if it is currently 0. If we tried to update `archived` first then it could get set to 1 and then subsequently the `archivedex` will get set too which is not what we want in your case (only one column is to be updated in the query).
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)