我有下一个sql查询:
SELECT
@row_number:=@row_number+1 AS ID, IGN_DATES.PLACA, IGN_DATES.FECHA, OFF_DATES.FECHA
FROM
(SELECT
a.PLACA AS PLACA, MIN(p.FECHA_GPS) AS FECHA
FROM
POSICIONHISTORIAL p
JOIN AUTOMOVIL a ON p.SECAUTOMOVIL = a.SECAUTOMOVIL
JOIN EVENTO e ON p.EVENTO_SECEVENTO = e.SECEVENTO
WHERE
p.FECHA_GPS BETWEEN '2016-09-01' AND '2016-10-01'
AND e.CODIGO = 'ENC'
AND a.PLACA IN ('SHS946', 'SHT011')
GROUP BY a.PLACA , DAY(p.FECHA_GPS)) AS IGN_DATES
JOIN
(SELECT
a.PLACA AS PLACA,
SUBSTRING_INDEX(GROUP_CONCAT(p.FECHA_GPS
ORDER BY p.FECHA_GPS DESC
SEPARATOR ' , '), ',', 2) AS FECHA
FROM
POSICIONHISTORIAL p
JOIN AUTOMOVIL a ON p.SECAUTOMOVIL = a.SECAUTOMOVIL
JOIN EVENTO e ON p.EVENTO_SECEVENTO = e.SECEVENTO
WHERE
p.FECHA_GPS BETWEEN '2016-09-01' AND '2016-10-01'
AND e.CODIGO = 'APA'
AND a.PLACA IN ('SHS946', 'SHT011')
GROUP BY a.PLACA , DAY(p.FECHA_GPS))
AS OFF_DATES ON DATE(IGN_DATES.FECHA) = DATE(OFF_DATES.FECHA),
(SELECT @row_number:=0) AS t;
我使用mysql 5.7,这个查询的结果是:
但是从JPA 2.1(Hibernate)调用的本机查询返回OFF_DATES.FECHA的重复值,这是来自eclipse调试的值。
声明中的rowid是意图解决问题的聚合,但这是不成功的
感谢您的帮助。
答案 0 :(得分:0)
我解决了这个问题,问题不在于One-To-Many relationship gets duplicate objects whithout using "distinct".Why?。
问题是用于连接的列,我添加了一列:
SELECT
@row_number:=@row_number+1 AS ID, IGN_DATES.PLACA, IGN_DATES.FECHA, OFF_DATES.FECHA
FROM
(SELECT
a.PLACA AS PLACA, MIN(p.FECHA_GPS) AS FECHA
FROM
POSICIONHISTORIAL p
JOIN AUTOMOVIL a ON p.SECAUTOMOVIL = a.SECAUTOMOVIL
JOIN EVENTO e ON p.EVENTO_SECEVENTO = e.SECEVENTO
WHERE
p.FECHA_GPS BETWEEN '2016-09-01' AND '2016-10-01'
AND e.CODIGO = 'ENC'
AND a.PLACA IN ('SHS946', 'SHT011')
GROUP BY a.PLACA , DAY(p.FECHA_GPS)) AS IGN_DATES
JOIN
(SELECT
a.PLACA AS PLACA,
SUBSTRING_INDEX(GROUP_CONCAT(p.FECHA_GPS
ORDER BY p.FECHA_GPS DESC
SEPARATOR ' , '), ',', 2) AS FECHA
FROM
POSICIONHISTORIAL p
JOIN AUTOMOVIL a ON p.SECAUTOMOVIL = a.SECAUTOMOVIL
JOIN EVENTO e ON p.EVENTO_SECEVENTO = e.SECEVENTO
WHERE
p.FECHA_GPS BETWEEN '2016-09-01' AND '2016-10-01'
AND e.CODIGO = 'APA'
AND a.PLACA IN ('SHS946', 'SHT011')
GROUP BY a.PLACA , DAY(p.FECHA_GPS))
AS OFF_DATES ON DATE(IGN_DATES.FECHA) = DATE(OFF_DATES.FECHA)
AND IGN_DATES.PLACA = OFF_DATES.PLACA,
(SELECT @row_number:=0) AS t;