0

Hello i have this sql

SELECT TOP 1 message,ticketid,Sender FROM Messages ORDER BY ID DESC WHERE ticketid = '2' 

Everytime i try to insert it , it gets me this error message

Error in query (1064): Syntax error near '1 message,ticketid,Sender FROM Messages ORDER BY ID DESC WHERE ticketid = '2'' at line 1

Btw table named as Messages got id,message,ticketid and Sender

Any idea how to fix it ?

Shadow
  • 33,525
  • 10
  • 51
  • 64
  • 2
    TOP is not MySQL syntax, and your clauses are all out of order in general. [The official online documentation](https://dev.mysql.com/doc/refman/8.0/en/select.html) is a good reference. – Uueerdo Jun 20 '18 at 22:18
  • And `WHERE` goes before `ORDER BY`. Study up a bit more on SQL syntax. – Gordon Linoff Jun 20 '18 at 22:19

1 Answers1

0

MSSql has TOP, MySql has LIMIT

So remove the TOP and add a LIMIT.
And also put the ORDER BY after the WHERE clause.

SELECT message, ticketid, Sender 
FROM Messages 
WHERE ticketid = '2'
ORDER BY ID DESC 
LIMIT 1

Also, when debugging, having your SQL all on 1 line will give a less meaningfull error about which line has the problem.

LukStorms
  • 28,916
  • 5
  • 31
  • 45
  • Thank you Sir for helping , i really did learn from you , i appriecte it – Titidone Jun 20 '18 at 22:31
  • another thing , do i put limit like this `LMIT 1` or like this `LIMIT '1'` in the $sql (php) – Titidone Jun 20 '18 at 22:33
  • @Titidone `LIMIT 1` should be fine. And if you want to skip records then know that something like `LIMIT 10 OFFSET 5` would skip the first 5 records. [This old SO post](https://stackoverflow.com/questions/10119291/) has an example of that. – LukStorms Jun 20 '18 at 22:44