重写将SQL查询加入子查询

时间:2015-06-08 08:02:30

标签: mysql inner-join

我无法将下面的join sql语句更改为子查询。请帮助。感谢

1.SELECT airports.NAME 
  FROM airports inner join 
       cities on cities.iata_code= airports.cty_iata_code INNER JOIN 
       FLIGHT_PROFILES ON AIRPORTS.IATA_CODE = FLIGHT_PROFILES.FROM 
  GROUP BY FLIGHT_PROFILES.FROM 
  ORDER BY COUNT(FLIGHT_PROFILES.FROM) DESC 
  LIMIT 1;

2.select c.Name, c.cou_iata_code as Country_Code 
  from cities c inner join 
       flight_profiles f on c.iata_code <> f.to 
  group by c.cou_iata_code 
  ORDER BY C.NAME asc;

3.select f.flt_numb as FLIGHT_NUMBER, a.name as DESTINATION_AP, a2.name as ALTERNATIVE_AP 
  from flight_profiles f inner join 
       airports a on f.to = a.iata_code inner join 
       airports a2 on a.cty_iata_code = a2.cty_iata_code 
  where a.iata_code != a2.iata_code;

1 个答案:

答案 0 :(得分:0)

原始第一个查询的初始修复。

              for (int i = 2; i <= upperLimit; i++){
                    if (primeList[i - 1] == true){
                        int lowest = m/i * i;

                        while (lowest < m){
                            lowest += i;
                        }
                        while (lowest <= n){
                            myPrimes[lowest - m] = false;
                            lowest += i;
                        }
                    }
                }

使用几个子查询。这可以获得每个&#39; FROM&#39;字段,然后获取计数的最大值。这与另一个子查询相结合,以获取每个FROM的所有计数,加入计数与最大计数匹配的位置。这样可以避免在查询中使用LIMIT(尽管如果两个城市的航班数量相同,它会带回2个城市)。

SELECT airports.NAME 
FROM airports 
INNER JOIN cities ON cities.iata_code = airports.cty_iata_code 
INNER JOIN FLIGHT_PROFILES ON AIRPORTS.IATA_CODE = FLIGHT_PROFILES.FROM 
GROUP BY airports.NAME 
ORDER BY COUNT(FLIGHT_PROFILES.`FROM`) DESC 
LIMIT 1;

在这种情况下,我建议避免使用子查询将是最有效和易于阅读的解决方案。

小记。您似乎有一个名为FROM的字段。我建议你通常避免使用任何SQL关键字的列名