Hvilken MySQL-dialekt bruger du?Hvis MySqlDialect eller MySql5Dialect kan du bruge følgende:
SELECT count(history.city_id) FROM history where timediff(now(), history.ts) < '720' and history.city_id = id
Eller definer en ny dvalefunktion
public class ExtendedMySQL5Dialect extends MySQL5Dialect
{
public ExtendedMySQL5Dialect()
{
super();
registerFunction( "date_sub_interval", new SQLFunctionTemplate( Hibernate.DATE, "date_sub(?1, INTERVAL ?2 ?3)" ) );
registerFunction( "date_add_interval", new SQLFunctionTemplate( Hibernate.DATE, "date_add(?1, INTERVAL ?2 ?3)" ) );
}
}
Forespørgsel:
History.ts < date_sub_interval(now(), 30, DAY)