确定最多航班数量的航线数量
我的桌子旅行币(trip_no,id_comp,plane,town_from,town_to,time_out,time_in)
我有这个查询,但是我需要选择不同的值,这些值不包含在同一个表的列中。
SELECT COUNT(trip_no) AS NumFlights,town_from,town_to
FROM trip
GROUP BY town_from,
town_to
ORDER BY numflights DESC;
这是我的结果集
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'
答案 0 :(得分:1)
在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;