SQL限制从

时间:2013-01-02 21:32:54

标签: mysql limit

我有这个查询

SELECT * FROM Posts WHERE Topics = '2hnsk6i2iqgoh0c6kdbyd' ORDER BY Number DESC limit 0,25

以下结果

2hnsk6i2iqgoh0c6kdbyd   admin   coecxrj0vqkow46h6243v   17:28   2013/01/02  1357147682
2hnsk6i2iqgoh0c6kdbyd   admin   uvde2od4fffjyri2se3v6   17:28   2013/01/02  1357147682
2hnsk6i2iqgoh0c6kdbyd   admin   7nvk4tamdcmmqxc8xxzns   17:28   2013/01/02  1357147682
2hnsk6i2iqgoh0c6kdbyd   admin   46ibv785bpbcqin5csgqt   17:28   2013/01/02  1357147681
2hnsk6i2iqgoh0c6kdbyd   admin   nprsukm2v7adx62psizcn   17:28   2013/01/02  1357147681
2hnsk6i2iqgoh0c6kdbyd   admin   5jffjmcrywv28ypxf88m8   17:28   2013/01/02  1357147681
2hnsk6i2iqgoh0c6kdbyd   admin   i5ejx2hqf7m6m4ybc0b43   17:28   2013/01/02  1357147680
2hnsk6i2iqgoh0c6kdbyd   admin   m72qzizi0ru4z6e2vz6vq   17:28   2013/01/02  1357147680
2hnsk6i2iqgoh0c6kdbyd   admin   3mf83zzvow6wt2gfhb6ub   17:28   2013/01/02  1357147680
2hnsk6i2iqgoh0c6kdbyd   admin   ct2j2u8xx8ny8x3u27sjc   17:27   2013/01/02  1357147679
2hnsk6i2iqgoh0c6kdbyd   admin   sdbx6iyburbeg0ie0magt   17:27   2013/01/02  1357147679
2hnsk6i2iqgoh0c6kdbyd   admin   5hir63ok3kq06g3upqbhk   17:27   2013/01/02  1357147679
2hnsk6i2iqgoh0c6kdbyd   admin   4pvuwgqazd84yck7adoxa   17:27   2013/01/02  1357147678
2hnsk6i2iqgoh0c6kdbyd   admin   oiv6quuyrpwe8bgirjr4i   17:27   2013/01/02  1357147678
2hnsk6i2iqgoh0c6kdbyd   admin   xcutah6426zesq323gfwx   17:27   2013/01/02  1357147678
2hnsk6i2iqgoh0c6kdbyd   admin   hgfqab040h4k55bjedpkt   17:27   2013/01/02  1357147678
2hnsk6i2iqgoh0c6kdbyd   admin   oonp5r3np5knm5d4ohm55   17:27   2013/01/02  1357147677
2hnsk6i2iqgoh0c6kdbyd   admin   cbckqm8qi2oaimir2epzi   17:27   2013/01/02  1357147677
2hnsk6i2iqgoh0c6kdbyd   admin   vtsaqex6wfdmh8ioceuvn   17:27   2013/01/02  1357147677
2hnsk6i2iqgoh0c6kdbyd   admin   0ubs8ur2wo55ohfuipdo0   17:27   2013/01/02  1357147677
2hnsk6i2iqgoh0c6kdbyd   admin   8tvorn65epoaict0fp26z   17:27   2013/01/02  1357147676
2hnsk6i2iqgoh0c6kdbyd   admin   soctqr26i7aycza2u73pi   17:27   2013/01/02  1357147676
2hnsk6i2iqgoh0c6kdbyd   admin   8ukjwmao2qtt374g6p7ej   17:27   2013/01/02  1357147676
2hnsk6i2iqgoh0c6kdbyd   admin   4wyinun2ki45h6ww73qg6   17:27   2013/01/02  1357147675
2hnsk6i2iqgoh0c6kdbyd   admin   3dpsbg2t7s7fj2hbiyenk   17:27   2013/01/02  1357147675

这会在一个主题中显示论坛帖子,但是说用户正在查看帖子历史记录,并希望使用它来转到主题,我想要一些代码,如

SELECT * 
FROM Posts 
WHERE Topics = '2hnsk6i2iqgoh0c6kdbyd' 
ORDER BY Number DESC limit (SELECT * FROM POSTS WHERE POST = 'soctqr26i7aycza2u73pi'),25.

那当然不起作用,但我不知道你是否可以,如果是的话,我怎么能够让这段代码工作

由于存在一些混淆,我会尝试并简化它。

我想运行查询主题='$ topic'然后在里面拉回Post ='$ post',这将返回一个值,我想要在限制中使用的行号作为返回数据的起点,或基本相似的代码来做同样的工作

MySQL版本5.5.29-log

2 个答案:

答案 0 :(得分:0)

least您要找的是什么?即25的较小者和对该帖子的回复数量?

SELECT * 
FROM Posts 
WHERE User = 'admin' 
ORDER BY Number DESC limit least((SELECT count(*) FROM POSTS WHERE POST = 'soctqr26i7aycza2u73pi'),25);

编辑:对OP所要求的内容存在误解。听起来像是通过帖子栏获取帖子号码,并获得接下来的25个帖子。

所以我认为OP实际上想要的是

select *
from posts
where user = 'admin'
order by number desc offset (select count(*) from posts where number > (select number from posts where post = 'soctqr26i7aycza2u73pi') )
limit 25;

请注意,由于OP的number >排序,我有<而不是desc


RE-EDIT:证明你不能在限制或偏移语句中做子选择(http://stackoverflow.com/questions/8181727/how-to-use-subquery-to-define-mysql-select-limit -offset)没有一些hacky魔法。

在讨论中,OP提到他正在使用PHP。 我的建议是将select count(*) from posts where number > (select number from posts where post = 'soctqr26i7aycza2u73pi'的结果存储为单独的变量,然后将其插入到偏移中,或者将最外层查询的结果从那里循环到25。

答案 1 :(得分:0)

SELECT * FROM Posts 
WHERE User = 'admin' 
AND Number >= ( SELECT Number FROM POSTS WHERE POST = 'soctqr26i7aycza2u73pi')
ORDER BY Number
LIMIT 25

这是问题的解决方案,感谢那些试图提供帮助的人