In MySQL, BOOL and BOOLEAN are synonyms for TINYINT(1). FALSE is an alias for 0 and TRUE is an alias for 1; in Boolean expressions, any nonzero non-NULL numeric value evaluates as true.
SELECT 1 IS TRUE, 0 IS FALSE, NULL IS UNKNOWN;
Output:
| 1 IS TRUE | 0 IS FALSE | NULL is UNKNOWN |
|---|---|---|
| 1 | 1 | 1 |
For strict two-state application flags, storing only 0 and 1 keeps the meaning clear.
SELECT * FROM plus2_boolean WHERE feb = TRUE;SELECT * FROM plus2_boolean WHERE feb = 1;
Output:
| name | jan | feb | mar |
|---|---|---|---|
| Ronn | 0 | 1 | 0 |
| Lone | 1 | 1 | 4 |
| Raju | NULL | 1 | 0 |
SELECT * FROM plus2_boolean WHERE feb = FALSE;
Output:
| name | jan | feb | mar |
|---|---|---|---|
| Alex | 1 | 0 | 1 |
| John | 1 | 0 | 2 |
Full sample table:
| name | jan | feb | mar |
|---|---|---|---|
| Alex | 1 | 0 | 1 |
| Ronn | 0 | 1 | 0 |
| John | 1 | 0 | 2 |
| Lone | 1 | 1 | 4 |
| Raju | NULL | 1 | 0 |
| King | NULL | NULL | 1 |
SELECT * FROM plus2_boolean WHERE mar IS TRUE;
Output:
| name | jan | feb | mar |
|---|---|---|---|
| Alex | 1 | 0 | 1 |
| John | 1 | 0 | 2 |
| Lone | 1 | 1 | 4 |
| King | NULL | NULL | 1 |
SELECT * FROM plus2_boolean WHERE jan IS NOT TRUE;
Output:
| name | jan | feb | mar |
|---|---|---|---|
| Ronn | 0 | 1 | 0 |
| Raju | NULL | 1 | 0 |
| King | NULL | NULL | 1 |
SELECT * FROM plus2_boolean WHERE jan IS UNKNOWN;
Output:
| name | jan | feb | mar |
|---|---|---|---|
| Raju | NULL | 1 | 0 |
| King | NULL | NULL | 1 |
IS TRUE / IS FALSE / IS UNKNOWN is useful when NULL needs to be distinguished from false.
SELECT * FROM plus2_boolean WHERE jan = feb;
Output:
| name | jan | feb | mar |
|---|---|---|---|
| Lone | 1 | 1 | 4 |
With ordinary =, comparisons involving NULL evaluate to NULL.
SELECT * FROM plus2_boolean WHERE jan <=> feb;
Output:
| name | jan | feb | mar |
|---|---|---|---|
| Lone | 1 | 1 | 4 |
| King | NULL | NULL | 1 |
The NULL-safe equality operator <=> returns 1 when both operands are NULL and 0 or 1 for other comparisons.
SELECT name,
IF(jan, 'OK', 'NOT OK') AS jan,
IF(feb, 'OK', 'NOT OK') AS feb,
IF(mar, 'OK', 'NOT OK') AS mar
FROM plus2_boolean;
Output:
| name | jan | feb | mar |
|---|---|---|---|
| Alex | OK | NOT OK | OK |
| Ronn | NOT OK | OK | NOT OK |
| John | OK | NOT OK | OK |
| Lone | OK | OK | OK |
| Raju | NOT OK | OK | NOT OK |
| King | NOT OK | NOT OK | OK |
SELECT name,
IF(jan, '<input type=checkbox checked>', '<input type=checkbox>') AS jan
FROM plus2_boolean;
Rendered checkbox example:
| name | jan | feb | mar |
|---|---|---|---|
| Alex | |||
| Ronn | |||
| John | |||
| Lone | |||
| Raju | |||
| King |
UPDATE plus2_boolean SET mar = TRUE WHERE name = 'Ronn';UPDATE plus2_boolean SET mar = 0 WHERE name = 'Ronn';UPDATE plus2_boolean SET mar = NOT mar WHERE name = 'Ronn';
INSERT INTO plus2_boolean (name, jan, feb, mar) VALUES
('Alex', 1, 0, 1), ('Ronn', 0, 1, 0);INSERT INTO plus2_boolean (name, jan, feb, mar) VALUES
('Alex2', TRUE, FALSE, TRUE), ('Ronn2', FALSE, TRUE, FALSE);
See the related checkbox data-matrix tutorial
Related operations: comparison operators, UPDATE, and INSERT.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.