java mysql order by,java-使用LIMIT和MySQL进行ORDER BY

我在使用MySQL查询时遇到问题,在该查询中我得到了一个带有LIMIT 1的行.但是,将其与order一起使用时,它不起作用.

在mysql工作台中运行的查询如下:

select * from train t

where t.togId = 1125

and t.tilDato >= '2013-12-20'

order by t.fraDato LIMIT 1;

但是,当我通过javacode和在我的服务器上运行此消息时,得到此stacktrace:

Exception Description:

Syntax error parsing [select t from train t where t.togId = :togId

and t.tilDato >= :todaysdate order by t.fraDato LIMIT 1].

[102, 103] The ORDER BY clause has 't.fraDato ' and 'LIMIT '

that are not separated by a comma.

[108, 109] The ORDER BY clause has 'LIMIT ' and '1' that are not separated by a comma.

查询是这样创建的:

Query query = em.createQuery("select t from train t where t.togId = :togId" +

" and t.tilDato >= :todaysdate order by t.fraDato LIMIT 1")

.setParameter("togId", togId)

.setParameter("todaysdate", new Date());

解决方法:

您似乎正在使用JPQL,即Java持久性查询语言. MySQL和JPQL(或Hibernate)是完全不同的查询语言,它们每个都有自己的特定语法. LIMIT构造仅在MySQL中可用,并且不属于任何SQL标准.通过在查询对象上设置最大结果数来模拟JPA中的功能.

因此,您应该使用LIMIT 1而不是LIMIT 1

query.setMaxResults(1);

标签:jpql,sql,java,mysql

来源: https://codeday.me/bug/20191030/1964949.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值