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.
查詢是這樣創(chuàng)建的:
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
總結
以上是生活随笔為你收集整理的java mysql order by,java-使用LIMIT和MySQL进行ORDER BY的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 小米air耳机重新配对_横比小米 Air
- 下一篇: golang web php,golan