/* Available in MySQL and PostgreSQL */
SELECT
Name,
CountryCode,
BIT_AND(Population) over p_code and_population,
BIT_OR(Population) over p_code or_population,
BIT_XOR(Population) over p_code xor_population
FROM city
WINDOW p_code AS (partition BY CountryCode)
| Name |
CountryCode |
and_population |
or_population |
xor_population |
| Oranjestad |
ABW |
29,034 |
29,034 |
29,034 |
| Herat |
AFG |
288 |
1,834,940 |
1,809,428 |
| Qandahar |
AFG |
288 |
1,834,940 |
1,809,428 |
| Kabul |
AFG |
288 |
1,834,940 |
1,809,428 |
| Mazar-e-Sharif |
AFG |
288 |
1,834,940 |
1,809,428 |
| Benguela |
AGO |
16,384 |
2,097,148 |
1,926,184 |
| Lobito |
AGO |
16,384 |
2,097,148 |
1,926,184 |
| Huambo |
AGO |
16,384 |
2,097,148 |
1,926,184 |
| Luanda |
AGO |
16,384 |
2,097,148 |
1,926,184 |
| ... |
| MySQL 的 BIT_AND() 是聚合函数,对列中所有值按位做 AND:两位都是 1 时结果为 1,否则为 0。
BIT_OR() 是聚合函数,对列中所有值按位做 OR:两位都是 0 时返回 0,否则返回 1。
BIT_XOR() 是聚合函数,对列中所有值按位做 XOR:只有一位是 1 时返回 1;两位都是 0 或都是 1 时返回 0。 |