mysql - 表中不同记录(行)的数量

标签 mysql sql

我经常需要计算表格中不同记录的数量。然而,在 MySQL 中都没有

select count(distinct *) from t;

也不

select count(distinct t.*) from t;

工作。我知道我可以解决这个问题

select count(*) as countdistinctrows
from (
  select distinct * from t
) x;

但这很丑陋。真的没有办法询问不同行的数量吗?顺便说一下,

select distinct count(*) from t;

不是答案,因为 distinct 应用于表中的行数,因此给出相同的结果

select count(*) from t;

最佳答案

如果你不想使用它:

select count(*) as countdistinctrows
from (
  select distinct * from t
) x;

我能想到的唯一选择是手动指定所有列名:

select count(distinct id, col1, col2, col3, ...)
from t;

关于mysql - 表中不同记录(行)的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36907817/

相关文章:

mysql 结果来自 table1,其中 table2 不匹配并且在 start_datetime 和 start_datetime + 8 小时之间

android - 将图像插入数据库并检索它

mysql - SQL脚本-用户连续亏损次数的计数器

MySQL 在存储过程中返回值

mysql - Laravel 验证 MSSQL

sql - Postgresql - 为每个 ID 返回 (N) 行

SQL 查询转置数据

python - 如何使用带有参数化占位符的 sqlite3 Python 库创建 SQLite 表?

mysql - Flask,尝试从 mysql 数据库读取数据(并非所有参数在字符串格式化期间都转换)

sql - 嵌套选择 - 多部分标识符未绑定(bind)错误