/* Available in MySQL and PostgreSQL */
SELECT
CountryCode,
BIT_AND(Population) and_population,
BIT_OR(Population) or_population,
BIT_XOR(Population) xor_population
FROM city
GROUP BY CountryCode
| CountryCode |
and_population |
or_population |
xor_population |
| ABW |
29,034 |
29,034 |
29,034 |
| AFG |
288 |
1,834,940 |
1,809,428 |
| AGO |
16,384 |
2,097,148 |
1,926,184 |
| AIA |
577 |
979 |
402 |
| ALB |
270,000 |
270,000 |
270,000 |
| AND |
21,189 |
21,189 |
21,189 |
| ANT |
2345 |
2345 |
2345 |
| ARE |
0 |
1,048,575 |
659,116 |
| ARG |
0 |
4,194,303 |
2,728,625 |
| ... |
| MySQL 的 BIT_AND() 是聚合函数,对列中所有值按位做 AND:两位都是 1 时结果为 1,否则为 0。
BIT_OR() 是聚合函数,对列中所有值按位做 OR:两位都是 0 时返回 0,否则返回 1。
BIT_XOR() 是聚合函数,对列中所有值按位做 XOR:只有一位是 1 时返回 1;两位都是 0 或都是 1 时返回 0。 |