在PostgreSQL中有一个名为"barn_animals";该表包括一个称为VARCHAR
型animal_type
的列,其中包括动物类型,如cat
、dog
、cow
、chicken
现在我只想将dog
和cat
查询到一个名为is_dog_not_cat
的bool
列中,而不考虑其他动物类型。因此我写道:
SELECT animal_type AS is_dog_not_cat
FROM barn_animals
WHERE animal_type IN ('dog', 'cat');
这只返回猫和狗作为VARCHAR
,但有没有办法将其强制转换为bool
,从而将dog
的True
和cat
的False
返回到is_dog_not_cat
列中,而不是dog
或cat
?谢谢
只需使用布尔表达式:
SELECT (animal_type = 'dog') AS is_dog_not_cat
FROM barn_animals
WHERE animal_type IN ('dog', 'cat');