在SQL中计算多列中的不同值对

时间:2021-12-04 04:23:10

What query will count the number of rows, but distinct by three parameters?

什么查询将计算行数,但由三个参数区分?

Example:

例:

Id      Name        Address 
==============================
1     MyName        MyAddress
2     MySecondName  Address2

Something like:

就像是:

select count(distinct id,name,address) from mytable

4 个解决方案

#1


22  

To get a count of the number of unique combinations of id, name and address:

要计算id,名称和地址的唯一组合数量:

SELECT Count(*)
FROM   (
        SELECT DISTINCT
               id
             , name
             , address
        FROM   your_table
       ) As distinctified

#2


9  

Get all distinct id, name and address columns and count the resulting rows.

获取所有不同的ID,名称和地址列并计算结果行。

SELECT COUNT(*) FROM mytable GROUP BY id, name, address

#3


5  

Another (probably not production-ready or recommended) method I just came up with is to concat the values to a string and count this string distinctively:

我想出的另一种(可能不是生产就绪或推荐)方法是将值连接到一个字符串并明确地计算这个字符串:

SELECT count(DISTINCT concat(id, name, address)) FROM mytable;

#4


0  

You can also do something like:

您还可以执行以下操作:

SELECT COUNT(DISTINCT id + name + address) FROM mytable

#1


22  

To get a count of the number of unique combinations of id, name and address:

要计算id,名称和地址的唯一组合数量:

SELECT Count(*)
FROM   (
        SELECT DISTINCT
               id
             , name
             , address
        FROM   your_table
       ) As distinctified

#2


9  

Get all distinct id, name and address columns and count the resulting rows.

获取所有不同的ID,名称和地址列并计算结果行。

SELECT COUNT(*) FROM mytable GROUP BY id, name, address

#3


5  

Another (probably not production-ready or recommended) method I just came up with is to concat the values to a string and count this string distinctively:

我想出的另一种(可能不是生产就绪或推荐)方法是将值连接到一个字符串并明确地计算这个字符串:

SELECT count(DISTINCT concat(id, name, address)) FROM mytable;

#4


0  

You can also do something like:

您还可以执行以下操作:

SELECT COUNT(DISTINCT id + name + address) FROM mytable