Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have the following scenario

+--------+--------+-----------------+-------------------------+
| Msgid |UserID | Msg | MsgDate |
+--------+--------+-----------------+-------------------------+
| 3 | 75 | TextMSG | 2014-04-01 12:05:29.957 |
| 4 | 75 | TextMSG | 2014-04-01 12:06:14.967 |
| 5 | 75 | TextMSG | 2014-04-01 12:06:48.160 |
| 6 | 80 | TextMSG | 2014-04-01 12:07:02.227 |
| 7 | 80 | TextMSG | 2014-04-01 12:07:07.763 |
+--------+---------+-------+----------------------------------+

and write the query to retrieve the 2 recent msg of User id 75.

SQL
SELECT Msg FROM TempNoti WHERE UserID = 75 LIMIT 2 ORDER BY MsgDate DESC;


But it gives the following error
Incorrect syntax near 'LIMIT'.


I using the tutorial from this site mysqltutorial.
Posted
Comments
Maciej Los 1-Apr-14 5:13am    
SQL or MySQL? It makes the difference!

If it is MySQL, try:
SQL
SELECT Msg
FROM TempNoti
WHERE UserID = 75
ORDER BY MsgDate DESC
LIMIT 2;


Please, use proper tag.
 
Share this answer
 
LIMIT must be the last thing in the SELECT Statement:
SQL
SELECT Msg FROM TempNoti WHERE UserID = 75 ORDER BY MsgDate DESC LIMIT 2
 
Share this answer
 
SQL
SELECT TOP 2 Msg FROM TempNoti WHERE UserID = 75 ORDER BY MsgDate DESC;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900