Ребят, подскажите пожалуйста, как можно решить подобное (без доп. запросов)
Есть запрос -
SQL |
UPDATE `adverts` SET `impressions` = impressions + 1, `impressions_remain` = impressions_remain - 1 WHERE `ad_id` IN (....) |
Цитата |
а impressions_remain если меньше нуля, то должно быть 0? |
SQL |
UPDATE `adverts` SET `impressions` = `impressions` + 1, `impressions_remain` = CASE `impressions_remain` WHEN 0 THEN 0 ELSE `impressions_remain` - 1 END WHERE `ad_id` IN (...) |
Цитата |
Subtraction between integer values, where one is of type UNSIGNED, produces an unsigned result by default. If the result would otherwise have been negative, it becomes the maximum integer value. If the NO_UNSIGNED_SUBTRACTION SQL mode is enabled, the result is negative. If the result of such an operation is used to update an UNSIGNED integer column, the result is clipped to the maximum value for the column type, or clipped to 0 if NO_UNSIGNED_SUBTRACTION is enabled. If strict SQL mode is enabled, an error occurs and the column remains unchanged. |
SQL |
SET SQL_MODE = 'NO_UNSIGNED_SUBTRACTION' |