posgrein子句转换为布尔值



在PostgreSQL中有一个名为"barn_animals";该表包括一个称为VARCHARanimal_type的列,其中包括动物类型,如catdogcowchicken
现在我只想将dogcat查询到一个名为is_dog_not_catbool列中,而不考虑其他动物类型。因此我写道:

SELECT animal_type AS is_dog_not_cat
FROM barn_animals
WHERE animal_type IN ('dog', 'cat');

这只返回猫和狗作为VARCHAR,但有没有办法将其强制转换为bool,从而将dogTruecatFalse返回到is_dog_not_cat列中,而不是dogcat?谢谢

只需使用布尔表达式:

SELECT (animal_type = 'dog') AS is_dog_not_cat
FROM barn_animals
WHERE animal_type IN ('dog', 'cat');

最新更新