SQL 交集 - 语法错误
SQL intersection - error in syntax
我正在尝试搜索从伦敦出发并到达巴黎的航班。有关城市的信息位于 Airport table,而出发和到达机场的缩写位于 Flight.
这是我的问题代码:
SELECT flightNo,flightCompany
FROM Flight
JOIN Airport
ON Airport.airportId=Flight.depAirport
WHERE Airport.city='London'
INTERSECT
SELECT flightNo,flightCompany
FROM Flight
JOIN Airport
ON Airport.airportId=Flight.arrAirport
WHERE Airport.city='Paris';
它 returns 3 个错误 -
- SQL 状态:HY000 错误代码:1000 SQL 语句
中的语法错误
- SQL 状态:HY000 错误代码:1000
- SQL 状态:HY000 错误代码:1000 语法错误,意外的 $end,期待 BETWEEN 或 IN 或 SQL_TOKEN_LIKE
改用 JOIN
重写查询怎么样?
SELECT f.flightNo, f.flightCompany
FROM Flight f JOIN
Airport ad
ON ad.airportId = f.depAirport AND ad.city = 'London' JOIN
Airport aa
ON aa.airportId = f.arrAirport AND aa.city = 'Paris';
或者,使用 EXISTS
?
SELECT f.*
FROM Flight f
WHERE EXISTS (SELECT 1
FROM Airport ad
WHERE ad.airportId = f.depAirport AND ad.city = 'London'
) AND
EXISTS (SELECT 1
FROM Airport aa
WHERE aa.airportId = f.arrAirport AND aa.city = 'Paris'
);
使用默认的 LibreOffice Base / HSQLDB 1.8 设置的引用规则和区分大小写很棘手。这是您需要做的:
SELECT "flightNo","flightCompany"
FROM "Flight"
JOIN "Airport"
ON "Airport"."airportId"="Flight"."depAirport"
WHERE "Airport"."city"='London'
INTERSECT
SELECT "flightNo","flightCompany"
FROM "Flight"
JOIN "Airport"
ON "Airport"."airportId"="Flight"."arrAirport"
WHERE "Airport"."city"='Paris';
或者更好的是,修改 Gordon 的答案如下:
SELECT F."flightNo", F."flightCompany"
FROM "Flight" F JOIN
"Airport" AD
ON AD."airportId" = F."depAirport" AND AD."city" = 'London' JOIN
"Airport" AA
ON AA."airportId" = F."arrAirport" AND AA."city" = 'Paris';
我正在尝试搜索从伦敦出发并到达巴黎的航班。有关城市的信息位于 Airport table,而出发和到达机场的缩写位于 Flight.
这是我的问题代码:
SELECT flightNo,flightCompany
FROM Flight
JOIN Airport
ON Airport.airportId=Flight.depAirport
WHERE Airport.city='London'
INTERSECT
SELECT flightNo,flightCompany
FROM Flight
JOIN Airport
ON Airport.airportId=Flight.arrAirport
WHERE Airport.city='Paris';
它 returns 3 个错误 -
- SQL 状态:HY000 错误代码:1000 SQL 语句 中的语法错误
- SQL 状态:HY000 错误代码:1000
- SQL 状态:HY000 错误代码:1000 语法错误,意外的 $end,期待 BETWEEN 或 IN 或 SQL_TOKEN_LIKE
改用 JOIN
重写查询怎么样?
SELECT f.flightNo, f.flightCompany
FROM Flight f JOIN
Airport ad
ON ad.airportId = f.depAirport AND ad.city = 'London' JOIN
Airport aa
ON aa.airportId = f.arrAirport AND aa.city = 'Paris';
或者,使用 EXISTS
?
SELECT f.*
FROM Flight f
WHERE EXISTS (SELECT 1
FROM Airport ad
WHERE ad.airportId = f.depAirport AND ad.city = 'London'
) AND
EXISTS (SELECT 1
FROM Airport aa
WHERE aa.airportId = f.arrAirport AND aa.city = 'Paris'
);
使用默认的 LibreOffice Base / HSQLDB 1.8 设置的引用规则和区分大小写很棘手。这是您需要做的:
SELECT "flightNo","flightCompany"
FROM "Flight"
JOIN "Airport"
ON "Airport"."airportId"="Flight"."depAirport"
WHERE "Airport"."city"='London'
INTERSECT
SELECT "flightNo","flightCompany"
FROM "Flight"
JOIN "Airport"
ON "Airport"."airportId"="Flight"."arrAirport"
WHERE "Airport"."city"='Paris';
或者更好的是,修改 Gordon 的答案如下:
SELECT F."flightNo", F."flightCompany"
FROM "Flight" F JOIN
"Airport" AD
ON AD."airportId" = F."depAirport" AND AD."city" = 'London' JOIN
"Airport" AA
ON AA."airportId" = F."arrAirport" AND AA."city" = 'Paris';