Spring 数据 Jpa JPQL 使用内部连接到 select 最低价假期
Spring Data Jpa JPQL using inner join to select minimum priced holiday
我试图从 table 中找到每个月从每个出发机场到目标目的地机场的最低价格假期,记录超过 100 万条。我已成功创建所需的 MySQL 查询:-
SELECT * FROM results r INNER JOIN (SELECT id,MIN(price) FROM results GROUP BY BulkLoaderRef) r2 ON r.id=r2.id ORDER BY r.departureDate, r.depAirportCode, r.resortID LIMIT 10
但是我找不到该查询的有效 JPQL 语法?
请帮忙?
我的table格式如下:-
CREATE TABLE `results` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`HotelReference` varchar(128) NOT NULL DEFAULT '',
`HotelName` varchar(64) NOT NULL DEFAULT '',
`Resort` varchar(128) NOT NULL DEFAULT '',
`Description` longtext NOT NULL,
`Photo` varchar(255) NOT NULL DEFAULT '',
`OfferProcessType` varchar(1) NOT NULL DEFAULT 'A',
`TradingName` varchar(32) NOT NULL DEFAULT 'Heffernans Travel',
`OfferLabelDesc` varchar(16) NOT NULL DEFAULT 'EasyHolidays',
`DestinationName` varchar(128) NOT NULL DEFAULT '',
`DepAirportCode` varchar(3) NOT NULL DEFAULT '',
`TelephoneNumber` varchar(16) NOT NULL DEFAULT '021 230 0700',
`ReturnAirportCode` varchar(3) NOT NULL DEFAULT '',
`ArrivalAirportCode` varchar(3) NOT NULL DEFAULT '',
`ResortID` int(11) NOT NULL,
`ABTA` varchar(16) NOT NULL DEFAULT 'ABTA',
`ATOL` varchar(16) NOT NULL DEFAULT 'ATOL',
`OtherBonding` varchar(64) NOT NULL DEFAULT 'Agent for ATOL protected operator',
`BoardTypesShort` varchar(2) NOT NULL DEFAULT '',
`AccomodationShort` varchar(3) NOT NULL DEFAULT 'Htl',
`AllocationShort` varchar(3) DEFAULT '""',
`OperatorShort` varchar(16) NOT NULL DEFAULT 'EasyHolidays',
`DepartureDate` date NOT NULL,
`DepartureMonth` varchar(2) DEFAULT NULL,
`DepartureYear` varchar(4) DEFAULT NULL,
`Price` int(11) NOT NULL,
`ShareBasisAdult` int(11) DEFAULT '2',
`BulkLoaderRef` varchar(64) DEFAULT NULL,
`Email` varchar(64) DEFAULT '',
`PublicNotes` varchar(64) DEFAULT 'EasyHolidays',
`CCC` varchar(32) DEFAULT ' Credit Card Charges',
`TOD` varchar(16) DEFAULT '',
`DCC` varchar(16) DEFAULT '',
`AMEX` varchar(16) DEFAULT '',
`CDW` varchar(16) DEFAULT '',
`BookingFee` double DEFAULT NULL,
`AccomodationName` varchar(64) NOT NULL DEFAULT '',
`duration` int(11) NOT NULL DEFAULT '0',
`ShareBasisChild` int(11) DEFAULT '2',
`Rating` varchar(16) NOT NULL DEFAULT '',
`OfferLink` varchar(16) NOT NULL DEFAULT '',
`AccomodationRef` varchar(128) NOT NULL DEFAULT '',
`Transfers` varchar(32) NOT NULL DEFAULT '',
`OperatorsRating` varchar(32) NOT NULL DEFAULT '',
`QuoteRef` varchar(64) NOT NULL DEFAULT '',
`Url` varchar(128) NOT NULL DEFAULT '',
`OutDepTime` varchar(16) NOT NULL DEFAULT '',
`OutArrTime` varchar(16) NOT NULL DEFAULT '',
`InDepTime` varchar(16) NOT NULL DEFAULT '',
`InArrTime` varchar(16) NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
UNIQUE KEY `HotelReference` (`HotelReference`),
KEY `BulkLoaderRef` (`BulkLoaderRef`),
KEY `Price` (`Price`)
) ENGINE=InnoDB AUTO_INCREMENT=1091073 DEFAULT CHARSET=utf8;
JPQL 不支持 JOIN 语句的子查询。话虽如此,我建议您将 JOIN 的子查询移动到视图中。那么就变成了一个简单的JPQL语句。
将子查询移动到视图中:
CREATE VIEW min_price_view AS
SELECT id,MIN(price) FROM results GROUP BY BulkLoaderRef;
然后在查询中替换:
SELECT
*
FROM
results r
INNER JOIN
min_price_view r2 ON r.id=r2.id
ORDER BY
r.departureDate, r.depAirportCode, r.resortID
LIMIT 10
至此,转成JPQL就变得很简单了
我试图从 table 中找到每个月从每个出发机场到目标目的地机场的最低价格假期,记录超过 100 万条。我已成功创建所需的 MySQL 查询:-
SELECT * FROM results r INNER JOIN (SELECT id,MIN(price) FROM results GROUP BY BulkLoaderRef) r2 ON r.id=r2.id ORDER BY r.departureDate, r.depAirportCode, r.resortID LIMIT 10
但是我找不到该查询的有效 JPQL 语法?
请帮忙?
我的table格式如下:-
CREATE TABLE `results` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`HotelReference` varchar(128) NOT NULL DEFAULT '',
`HotelName` varchar(64) NOT NULL DEFAULT '',
`Resort` varchar(128) NOT NULL DEFAULT '',
`Description` longtext NOT NULL,
`Photo` varchar(255) NOT NULL DEFAULT '',
`OfferProcessType` varchar(1) NOT NULL DEFAULT 'A',
`TradingName` varchar(32) NOT NULL DEFAULT 'Heffernans Travel',
`OfferLabelDesc` varchar(16) NOT NULL DEFAULT 'EasyHolidays',
`DestinationName` varchar(128) NOT NULL DEFAULT '',
`DepAirportCode` varchar(3) NOT NULL DEFAULT '',
`TelephoneNumber` varchar(16) NOT NULL DEFAULT '021 230 0700',
`ReturnAirportCode` varchar(3) NOT NULL DEFAULT '',
`ArrivalAirportCode` varchar(3) NOT NULL DEFAULT '',
`ResortID` int(11) NOT NULL,
`ABTA` varchar(16) NOT NULL DEFAULT 'ABTA',
`ATOL` varchar(16) NOT NULL DEFAULT 'ATOL',
`OtherBonding` varchar(64) NOT NULL DEFAULT 'Agent for ATOL protected operator',
`BoardTypesShort` varchar(2) NOT NULL DEFAULT '',
`AccomodationShort` varchar(3) NOT NULL DEFAULT 'Htl',
`AllocationShort` varchar(3) DEFAULT '""',
`OperatorShort` varchar(16) NOT NULL DEFAULT 'EasyHolidays',
`DepartureDate` date NOT NULL,
`DepartureMonth` varchar(2) DEFAULT NULL,
`DepartureYear` varchar(4) DEFAULT NULL,
`Price` int(11) NOT NULL,
`ShareBasisAdult` int(11) DEFAULT '2',
`BulkLoaderRef` varchar(64) DEFAULT NULL,
`Email` varchar(64) DEFAULT '',
`PublicNotes` varchar(64) DEFAULT 'EasyHolidays',
`CCC` varchar(32) DEFAULT ' Credit Card Charges',
`TOD` varchar(16) DEFAULT '',
`DCC` varchar(16) DEFAULT '',
`AMEX` varchar(16) DEFAULT '',
`CDW` varchar(16) DEFAULT '',
`BookingFee` double DEFAULT NULL,
`AccomodationName` varchar(64) NOT NULL DEFAULT '',
`duration` int(11) NOT NULL DEFAULT '0',
`ShareBasisChild` int(11) DEFAULT '2',
`Rating` varchar(16) NOT NULL DEFAULT '',
`OfferLink` varchar(16) NOT NULL DEFAULT '',
`AccomodationRef` varchar(128) NOT NULL DEFAULT '',
`Transfers` varchar(32) NOT NULL DEFAULT '',
`OperatorsRating` varchar(32) NOT NULL DEFAULT '',
`QuoteRef` varchar(64) NOT NULL DEFAULT '',
`Url` varchar(128) NOT NULL DEFAULT '',
`OutDepTime` varchar(16) NOT NULL DEFAULT '',
`OutArrTime` varchar(16) NOT NULL DEFAULT '',
`InDepTime` varchar(16) NOT NULL DEFAULT '',
`InArrTime` varchar(16) NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
UNIQUE KEY `HotelReference` (`HotelReference`),
KEY `BulkLoaderRef` (`BulkLoaderRef`),
KEY `Price` (`Price`)
) ENGINE=InnoDB AUTO_INCREMENT=1091073 DEFAULT CHARSET=utf8;
JPQL 不支持 JOIN 语句的子查询。话虽如此,我建议您将 JOIN 的子查询移动到视图中。那么就变成了一个简单的JPQL语句。
将子查询移动到视图中:
CREATE VIEW min_price_view AS
SELECT id,MIN(price) FROM results GROUP BY BulkLoaderRef;
然后在查询中替换:
SELECT
*
FROM
results r
INNER JOIN
min_price_view r2 ON r.id=r2.id
ORDER BY
r.departureDate, r.depAirportCode, r.resortID
LIMIT 10
至此,转成JPQL就变得很简单了