Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this the proper way to do boolean test in SQL?

Assume active is a "boolean field" (tiny int, with 0 or 1)

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

In words, can the "NOT" operator be applied directly on the boolean field?

like image 929
Eric Avatar asked May 13 '09 18:05

Eric


People also ask

How do you do a boolean in SQL?

You can insert a boolean value using the INSERT statement: INSERT INTO testbool (sometext, is_checked) VALUES ('a', TRUE); INSERT INTO testbool (sometext, is_checked) VALUES ('b', FALSE); When you select a boolean value, it is displayed as either 't' or 'f'.

Does SQL allow boolean?

BOOLEAN can be used as a data type when defining a column in a table or a variable in a database procedure. Support for the BOOLEAN data type helps migrations from other database products. Boolean columns accept as input the SQL literals FALSE and TRUE.

How can you set boolean true SQL?

You can update boolean value using UPDATE command. If you use the BOOLEAN data type, MySQL internally convert it into tinyint(1). It can takes true or false literal in which true indicates 1 to tinyint(1) and false indicates 0 to tinyint(1).

Is true boolean SQL?

The BOOLEAN type represents a statement of truth (“true” or “false”). In SQL, the boolean field can also have a third state “unknown” which is represented by the SQL NULL value.


1 Answers

A boolean in SQL is a bit field. This means either 1 or 0. The correct syntax is:

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

or

select * from users where active = 0 /* All Inactive Users */ 
like image 192
Jose Basilio Avatar answered Sep 18 '22 21:09

Jose Basilio