bool_and
The bool_and() function calculates all the boolean values in the aggregated group, which will have these results:
-
trueif all the values aretruefor every row. -
falseif at least one row in the group isfalse.
The input and the return type must be in bool.
|
|
Examples
This example uses a payment table that stores details of the orders, whether the order has been paid or unpaid by the customer:
CREATE TABLE payment (
orderid int,
custname text,
orderproduct text,
ordertotal real,
paid boolean
);
INSERT INTO payment (orderid, custname, orderproduct, ordertotal, paid)
VALUES
(9557411, 'Maya', 'Jeans', 10.5, true),
(9557421, 'Aaron', 'T-Shirt', 9.2, true),
(9557451, 'Alex', 'Hat', 10.8, true),
(9557311, 'Will', 'Hat', 8.5, true),
(9557321, 'Will', 'T-Shirt', 12.15, true),
(9557351, 'Maya', 'T-Shirt', 9.5, true),
(9557221, 'Maya', 'Jeans', 11.02, true),
(9557251, 'Alex', 'Jeans', 11.09, true),
(9557231, 'Aaron', 'Hat', 14.56, false),
(9557281, 'Aaron', 'Hat', 12.15, true),
(9557291, 'Will', 'T-Shirt', 13.1, true);
SELECT * FROM payment;
This query shows the following table:
+----------+-----------+---------------+-------------+-------+
| orderid | custname | orderproduct | ordertotal | paid |
+----------+-----------+---------------+-------------+-------+
| 9557411 | Maya | Jeans | 10.5 | t |
| 9557421 | Aaron | T-Shirt | 9.2 | t |
| 9557451 | Alex | Hat | 10.8 | t |
| 9557311 | Will | Hat | 8.5 | t |
| 9557321 | Will | T-Shirt | 12.15 | t |
| 9557351 | Maya | T-Shirt | 9.5 | t |
| 9557221 | Maya | Jeans | 11.02 | t |
| 9557251 | Alex | Jeans | 11.09 | t |
| 9557231 | Aaron | Hat | 14.56 | f |
| 9557281 | Aaron | Hat | 12.15 | t |
| 9557291 | Will | T-Shirt | 13.1 | t |
+----------+-----------+---------------+-------------+-------+
bool_and with a false result
To find out if all customers have paid for their orders, run the query:
SELECT BOOL_AND(paid) AS "final_result" FROM payment;
In the bool_and function, if there is at least one FALSE value, the overall result is FALSE. The output shows that there is an order that hasn’t been paid.
+--------------+
| final_result |
+--------------+
| f |
+--------------+
bool_and with a true result
To find out if Maya has paid for her orders, run the query:
SELECT BOOL_AND(paid) AS Maya_Paid
FROM payment
WHERE custname ='Maya';
In the bool_and function, if all values are TRUE, then the overall result is TRUE. The output shows that Maya has paid all her orders.
+-----------+
| maya_paid |
+-----------+
| t |
+-----------+