89

active が「ブール フィールド」(0 または 1 の小さな int) であると仮定します。

# Find all active users
select * from users where active 

# Find all inactive users
select * from users where NOT active 

つまり、「NOT」演算子をブールフィールドに直接適用できますか?

4

7 に答える 7

97

SQL のブール値はビット フィールドです。これは、1 または 0 のいずれかを意味します。正しい構文は次のとおりです。

select * from users where active = 1 /* All Active Users */

また

select * from users where active = 0 /* All Inactive Users */
于 2009-05-13T19:01:10.127 に答える
30

Postgresでは、次を使用できます

select * from users where active

また

select * from users where active = 't'

整数値を使用する場合は、それを文字列と見なす必要があります。整数値は使用できません。

select * from users where active = 1   -- Does not work

select * from users where active = '1' -- Works 
于 2009-05-13T19:13:14.053 に答える
14

MS SQL 2008 では、true または false の文字列バージョンも使用できます...

select * from users where active = 'true'
-- or --
select * from users where active = 'false'
于 2009-05-13T19:08:05.187 に答える
12

SQL Server では、一般的に使用します。他のデータベース エンジンについては知りません。

select * from users where active = 0
于 2009-05-13T19:00:54.993 に答える
3

個人的には、boolean のネイティブ型を持たないデータベースに対して、値 'Y' と 'N' を持つ char(1) を使用することを好みます。文字は、1 が true に対応し、0 が false に対応するようになったので、それを読んだ人がそうするだろうと想定している数字よりもユーザーフレンドリーです。

(N)Hibernate を使用すると、'Y' と 'N' も適切にマップされます。

于 2009-05-13T19:30:52.793 に答える