简体   繁体   中英

Convert SQL statement to JPQL statement

I wonder how I convert this SQL statement to JPQL:

SELECT sum(price) AS Income, count(*) AS Passages, Station.Name 
FROM Passage
INNER JOIN station ON passage.stationId = station.id
GROUP BY Station.Name

wouldnt it be something like this:

SELECT sum(price) AS Income, count(p) AS Passages, s.Name FROM Passage p
INNER JOIN station s     
ON p.stationId = s.id GROUP BY s.Name

?

Problem solved with:

SELECT count(p), sum(p.price), s 
FROM Passage p 
INNER JOIN p.station s 
GROUP BY s.name;

There is no ON in JPQL (JPA 2.0), but you can do an implicit join and use the WHERE clause:

Assuming that the entity Passage the atriburo StationID is the same type of object that the attribute id of the entity Station .

SELECT SUM(s.price) Income, COUNT(p) Passages, s.Name
FROM Passage p, Station s
WHERE p.stationId = s.id
GROUP BY s.Name

It is recommended that if the question can add entities Station Passage and to give a correct answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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