我需要比较表中的一些值,即如果更少,更大和相等......
我的查询如下:
SELECT `t1`.`K1`
IF (Sum(`t1`.`K1`) > Sum(`t2`.`K1`), 'Greater', 'Less') AS State
FROM `t1`
INNER JOIN `t2` ON `t1`.`Code` = `t2`.`Code`
GROUP BY `t1`.`K1`
如果值相等,如何实现条件?
答案 0 :(得分:3)
SELECT `t1`.`K1`,
case when Sum(`t1`.`K1`) = Sum(`t2`.`K1`) then 'Equal'
when Sum(`t1`.`K1`) > Sum(`t2`.`K1`) then 'Greater'
else 'Less'
end AS State
FROM `t1`
INNER JOIN `t2` ON `t1`.`Code` = `t2`.`Code`
GROUP BY `t1`.`K1`
答案 1 :(得分:1)
SELECT `t1`.`K1`,
IF (Sum(`t1`.`K1`) = Sum(`t2`.`K1`), 'Equal', IF (Sum(`t1`.`K1`) > Sum(`t2`.`K1`), 'Greater', 'Less')) AS State
FROM `t1`
INNER JOIN `t2` ON `t1`.`Code` = `t2`.`Code`
GROUP BY `t1`.`K1`