I am trying to find best query, but am getting an exception all the time.
That is my native SQL code with hibernate:
String query_string = "SELECT g.id AS id, g.createDate AS createDate,g.name AS name, g.serial AS serial, MAX(g.count) AS count, MAX(g.serviceCount) AS serviceCount, g.reportCardNo as reportCardNo " +
"FROM GameTransaction g" +
"WHERE DATE_FORMAT(createDate,'%Y-%m-%d %H:%i') BETWEEN '" + simple_date_format.format(begin_date) + " 00:00' AND '" + simple_date_format.format(end_date) + " 23:59'" +
"GROUP BY g.createDate, g.serial " +
"ORDER BY g.createDate DESC";
Query query = session.createSQLQuery(query_string)
.addScalar("count", new IntegerType())
.addScalar("createDate",new TimestampType())
.addScalar("serial", new StringType())
.addScalar("serviceCount", new IntegerType())
.addScalar("reportCardNo", new StringType())
.addScalar("name", new StringType())
.addScalar("id", new LongType())
.setResultTransformer(Transformers.aliasToBean(GameTransaction.class));
List<GameTransaction> pts = query.list();
And I am taking this exception:
"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DATE_FORMAT(createDate,'%Y-%m-%d %H:%i') BETWEEN '2014-10-14 00:00' AND '2014-10' at line 1"
When I change the SQL code exception changes as well For example:
"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'BY g.createDate, g.serial ORDER BY g.createDate DESC' at line 1"
BETWEEN '2014-10-14 00:00' AND '2014-10'
is this a typo ? should beBETWEEN '2014-10-14 00:00' AND '2014-10-14 23:00'
– PeterMmm 17 hours ago