SA-MP Forums Archive
Help with construction of one update MySQL query - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Help with construction of one update MySQL query (/showthread.php?tid=651060)



Help with construction of one update MySQL query - Riwerry - 12.03.2018

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.


Re: Help with construction of one update MySQL query - Maximun - 12.03.2018

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



Re: Help with construction of one update MySQL query - Riwerry - 12.03.2018

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


Re: Help with construction of one update MySQL query - mickmelon - 13.03.2018

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