简体   繁体   中英

Select unique values of 3 columns from a sql table which has 7 columns

This is what my table looks like.

I want to get unique 'sets' of origin, destination, and flight_date.

So, for the table shown, my query should return {['BLR','DEL', '2019-01-10'], ['BLR','DEL', '2019-01-11']}

In MySQL this should do the trick:

SELECT DISTINCT origin, destination, flight_date FROM flights

Alternatively:

SELECT origin, destination, flight_date 
FROM flights
GROUP BY origin, destination, flight_date

DISTINCT basically means "Make sure the rows are unique", wheras the advantage of using GROUP BY is, that you can use aggregate functions with it (summing, average etc). Also you can add additional conditions using HAVING.

Example:

SELECT origin, destination, flight_date, SUM(time_bucket) AS time_bucket_total
FROM flights
GROUP BY origin, destination, flight_date
HAVING SUM(time_bucket) > 3

This will return the summed time_bucket values along each row, and will also return only those entries where that sum is greater than 3.

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