简体   繁体   English

如何在MySQL数据库管理系统中的多个列上使用“ SQL SELECT DISTINCT语句”?

[英]How to use “SQL SELECT DISTINCT Statement” on multiple columns specifically in MySQL database management system?

I know how to use "SQL SELECT DISTINCT Statement" on a single column as below : 我知道如何在单列上使用“ SQL SELECT DISTINCT语句”,如下所示:

SELECT DISTINCT City FROM Customers;

But what about using "SQL SELECT DISTINCT Statement" on multiple columns(PostalCode and Country are the two other desired columns) where I want different(ie DISTINCT) data elements from to each of the desired columns? 但是,如果要在多个列(PostalCode和Country是另外两个所需的列)上使用“ SQL SELECT DISTINCT语句”,我想在每个所需的列中使用不同的(即DISTINCT)数据元素呢?

I want the answer specific to MySQL database. 我想要特定于MySQL数据库的答案。

I checked on stackoverflow. 我检查了stackoverflow。 The questions like these have been already asked but the have been answered bt keeping in mind the MS SQL Server database. 已经问过类似这样的问题,但要记住,要记住MS SQL Server数据库。 So, I've asked this question specifically oriented to MySQL database system. 因此,我问了这个专门针对MySQL数据库系统的问题。

Please provide me in detail solution with appropriate explanation. 请为我提供详细的解决方案并提供适当的说明。

Thanks. 谢谢。

You can use 您可以使用

select distinct PostalCode ,Country 
from
yourtable

Note the Above distinct implies unique combination of postalcode,country not unique combination of postalcode and unique combination of country 请注意,以上区别暗示postalcode,country唯一组合postalcode,country不是postalcode唯一组合和country唯一组合

You can use something like: SELECT DISTINCT(CONCAT(col1, col2, ...)) FROM Table; 您可以使用类似以下内容的内容: SELECT DISTINCT(CONCAT(col1, col2, ...)) FROM Table; Or SELECT col1, col2, ... FROM Table 1 group by 1, 2, ... Not sure which is performing better though. SELECT col1, col2, ... FROM Table 1 group by 1, 2, ...但是不确定哪个执行得更好。

当您使用distinct时,请确保仅选择字段并且具有如下所示的冗余值:

Select  distinct fieldshas_duplicate_one,fieldshas_duplicate_two from table;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM