我有点困惑为什么我在以下示例中得到结果。让我们假设我们想要对有多喜欢动物的人进行排名。他们喜欢任何有色老虎,红色动物第二,并不喜欢任何其他。
考虑下表:
----------------------
| Color | Animal |
----------------------
| Yellow | Butterfly |
| Red | Lion |
| Red | Tiger |
| Green | Lion |
| Green | Donkey |
| Yellow | Tiger |
----------------------
现在,当我运行此代码时:
CREATE VIEW animal_colors as select "farm"."color" as "color",
CASE WHEN "farm"."color" = 'Red' THEN 1 ELSE 0 END AS "color_red",
CASE WHEN "farm"."animal" = 'Tiger' THEN 1 ELSE 0 END AS "animal_tiger",
CASE
WHEN "animal_tiger" = 1 THEN 1
WHEN "color_red" = 1 THEN 0
ELSE -1
END
AS "rank"
FROM "farm"
我得到以下输出:
----------------------------------------------------------
| Color | Animal | color_red | animal_tiger | rank |
---------------------------------------------------------
| Yellow | Butterfly | 0 | 0 | -1 |
| Red | Lion | 1 | 0 | -1 |
| Red | Tiger | 1 | 1 | -1 |
| Green | Lion | 0 | 0 | -1 |
| Green | Donkey | 0 | 0 | -1 |
| Yellow | Tiger | 0 | 1 | -1 |
---------------------------------------------------------
我以为我正在比较变量color_red和animal_tiger以查看它们是否为1,但看起来并非如此。任何人都可以解释为什么表达式总是评估为-1?