Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> MySQL

Đếm số điện thoại duy nhất từ cột BIGINT trong MySQL

Dùng COUNT(DISTINCT cot). Tạo bảng:

mysql> CREATE TABLE DemoTable (
    PhoneNumber BIGINT
); Query OK, 0 rows affected (1.29 sec)

Chèn dữ liệu (có trùng lặp):

mysql> INSERT INTO DemoTable VALUES(8567789898); Query OK, 1 row affected (0.94 sec)
mysql> INSERT INTO DemoTable VALUES(8567789898); Query OK, 1 row affected (0.34 sec)
mysql> INSERT INTO DemoTable VALUES(9876564534); Query OK, 1 row affected (0.43 sec)
mysql> INSERT INTO DemoTable VALUES(9087896545); Query OK, 1 row affected (0.28 sec)
mysql> INSERT INTO DemoTable VALUES(7656456589); Query OK, 1 row affected (0.21 sec)
mysql> INSERT INTO DemoTable VALUES(9876564534); Query OK, 1 row affected (0.12 sec)
mysql> INSERT INTO DemoTable VALUES(9087896545); Query OK, 1 row affected (0.18 sec)

Hiển thị dữ liệu:

mysql> SELECT * FROM DemoTable;

Kết quả (7 hàng, 4 giá trị duy nhất):

+-------------+
| PhoneNumber |
+-------------+
| 8567789898  |
| 8567789898  |
| 9876564534  |
| 9087896545  |
| 7656456589  |
| 9876564534  |
| 9087896545  |
+-------------+
7 rows in set (0.00 sec)

Truy vấn đếm số điện thoại duy nhất:

mysql> SELECT COUNT(DISTINCT PhoneNumber) AS UniquePhoneNumber FROM DemoTable;

Kết quả:

+-------------------+
| UniquePhoneNumber |
+-------------------+
| 4                 |
+-------------------+
1 row in set (0.00 sec)