SELECT a.name , b.name , **(a.marks - b.marks ) AS "difference"**
FROM a , b
WHERE a.marks - b.marks < 1 AND a.marks - b.marks > -1 AND a.marks2 - b.marks2 < 1 AND a.marks2 - b.marks2 > -1
ORDER BY `difference` ASC
答案 0 :(得分:1)
您可以尝试返回绝对值的ABS()
来自MySql手册:
ABS(X)
返回X的绝对值。
的MySQL&GT; SELECT ABS(2);
-> 2
的MySQL&GT; SELECT ABS(-32);
-> 32
所以:
SELECT ABS(-5) + ABS(5)
Return -> 10
SELECT ABS(5-15)
Return -> 10
ABS()也可用于变量:
SET @Var :=-5;
SELECT ABS(@Var) + ABS(5)
Return -> 10
答案 1 :(得分:0)
如果我理解你想要什么,你可以尝试if()函数,如下所述:
Select a.name, b.name, if((a.marks - b.marks) < 0,((a.marks-b.marks)*(-1), (a.marks-b.marks)) as difference
from a , b ....