简体   繁体   中英

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 :

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?

I want the answer specific to MySQL database.

I checked on stackoverflow. The questions like these have been already asked but the have been answered bt keeping in mind the MS SQL Server database. So, I've asked this question specifically oriented to MySQL database system.

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

You can use something like: 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.

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

Select  distinct fieldshas_duplicate_one,fieldshas_duplicate_two from table;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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