简体   繁体   English

选择不同的值,这些值不包含在同一表的列中

[英]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 A – B和B – A被视为同一条路线
  2. Use only trip table 仅使用行程表

My Table Trip cointains (trip_no, id_comp, plane, town_from, town_to, time_out, time_in) 我的Table Trip硬币(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() : 在MySQL中,您可以使用least()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;

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

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