我的SQL Server中的LIMIT子句问题 [英] LIMIT Clause Problem in My SQL Server

查看:116
本文介绍了我的SQL Server中的LIMIT子句问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下情况



+ -------- + -------- + ------- ---------- + ------------------------- +

| Msgid |用户ID |消息| 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 |

+ -------- + --------- + ------- + --- ------------------------------- +



并写用于检索用户ID 75的最近2个消息的查询。



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





但它出现以下错误

'LIMIT'附近的语法不正确。





我使用本网站的教程mysqltutorial

解决方案

如果是MySQL,请尝试:

< pre lang =SQL> SELECT 消息
FROM TempNoti
WHERE UserID = 75
ORDER BY MsgDate DESC
LIMIT 2 ;





请使用正确的标签。


LIMIT必须是SELECT语句中的最后一项:

  SELECT 消息 FROM  TempNoti  WHERE  UserID =  75   ORDER   BY  MsgDate  DESC  LIMIT  2  


  SELECT   TOP   2 消息 FROM  Te mpNoti  WHERE  UserID =  75   ORDER   BY  MsgDate  DESC ; 


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.

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.

解决方案

If it is MySQL, try:

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



Please, use proper tag.


LIMIT must be the last thing in the SELECT Statement:

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


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


这篇关于我的SQL Server中的LIMIT子句问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆