开发者

Issue writing MySQL query to find difference

开发者 https://www.devze.com 2023-01-20 18:26 出处:网络
I\'m having trouble with sorting out a que开发者_StackOverflow社区ry for the following: Data: Column1Column2

I'm having trouble with sorting out a que开发者_StackOverflow社区ry for the following:

Data:
Column1  Column2
2        0
0        -2

I'm trying to select the difference between Column1 and Column2, with a minimum of 0. Eg.

Row1=2
Row2=0

My current query is SELECT (Column1 - Column2) as total FROM blah.

I tried adding max(Column2, 0) into the query, but that just errors.


Try:

 SELECT GREATEST(Column1 - Column2, 0)
 from Table


The MySQL function MAX() is an aggregate function, often used in combination with a GROUP BY. It only takes one argument (the name of the column of which you want to select the maximum value). The GREATEST() function is the function you need (as Michael Pakhantsov pointed out).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号