0

I want to execute this query using native query

SELECT name FROM `question`
ORDER BY RAND()
LIMIT 20

but when executing it i'm getting thgis error

Exception Description: Syntax error parsing the query [Question.findrandom: SELECT q FROM Question q ORDER BY RAND()], line 1, column 38: unexpected token [(].

I also have the entity created for the table question

Mahmoud Gamal
  • 78,257
  • 17
  • 139
  • 164
user2327579
  • 449
  • 9
  • 24

2 Answers2

0

Order by command in sql can only be used by asc(Asending) or desc(Descending) order so your query should be like

 SELECT name FROM `question`
     ORDER BY asc/desc
           LIMIT 20
Ankur
  • 5,086
  • 19
  • 37
  • 62
alok.kumar
  • 380
  • 3
  • 11
0

Use the createNativeQuery of EntityManager to create your native query:

Query query = em.createNativeQuery("SELECT name FROM question ORDER BY RAND() LIMIT 20");

where em is your EntityManager

Paolo
  • 1,641
  • 11
  • 15