0

我有这个查询

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.

那当然行不通,但是我不知道您是否可以,如果可以,我将如何使那段代码正常工作

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

我想运行查询 Topics = '$topic' 然后在那个 pull Post = '$post' 中,这将返回一个值,我希望在 Limit 中使用它的行号作为起点返回数据或本质上相似的代码来做同样的工作

MySQL 版本 5.5.29-日志

4

2 回答 2

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;

请注意,我有number >而不是<因为 OP 的desc订购。


重新编辑:原来你不能在限制或偏移语句中进行子选择(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。

于 2013-01-02T22:04:56.793 回答
0
SELECT * FROM Posts 
WHERE User = 'admin' 
AND Number >= ( SELECT Number FROM POSTS WHERE POST = 'soctqr26i7aycza2u73pi')
ORDER BY Number
LIMIT 25

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

于 2013-01-03T01:15:49.433 回答