简体   繁体   中英

Select different values, which are not included in a column of the same table

Help

Determine number of routes which are served by the greatest number of flights

  1. A – B and B – A are to be considered the same route
  2. Use only trip table

My Table Trip cointains (trip_no, id_comp, plane, town_from, town_to, time_out, time_in)

I have this Query, but i need to select different values, which are not included in a column of the same table.

SELECT COUNT(trip_no) AS NumFlights,town_from,town_to 
FROM   trip
GROUP  BY town_from, 
          town_to 
ORDER  BY numflights DESC;

and this is my result set

NumFlights, town_from, town_to
'4', 'Moscow', 'Rostov'
'4', 'London', 'Singapore'
'4', 'Rostov', 'Moscow'
'4', 'Singapore', 'London'
'1', 'Paris', 'Rostov'
'1', 'Paris', 'London'
'1', 'Vladivostok', 'Rostov'
'1', 'Rostov', 'Paris'
'1', 'London', 'Paris'
'1', 'Rostov', 'Vladivostok'

In MySQL, you can do what you want using least() and greatest() :

SELECT COUNT(trip_no) AS NumFlights,
       LEAST(town_from, town_to) as town_1, 
       GREATEST(town_from, town_to) as town_2
FROM trip
GROUP BY LEAST(town_from, town_to), 
         GREATEST(town_from, town_to)
ORDER BY numflights DESC;

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