MySQL 按顺序下一行
我有这样的查询:
SELECT id, name, town, street, number, number_addition, telephone
FROM clients
WHERE ((postalcode >= 'AABB' AND postalcode <= 'AACC') OR (postalcode >= 'DDEE' AND postalcode <= 'DDFF'))
ORDER BY town ASC, street ASC, number ASC, number_addition ASC
LIMIT 1
这样我可以获得第一个客户。
然后我想要获取下一个客户端(假设我知道当前客户端的 ID 为 58,并且我想要获取序列中的下一个客户端 - 我将有一个标记为当前的客户端,并且我想要获取下一个/上一个客户端)并且我已经知道第一个客户的 ID,您能告诉我如何实现这一点吗? (具有相同的顺序)
我发现了这个 http://www.artfulsoftware.com/infotree/ requests.php#75 但当我需要按多列排序时,我不知道如何将这些示例转换为命令。
感谢您抽出时间。
I have a query like this:
SELECT id, name, town, street, number, number_addition, telephone
FROM clients
WHERE ((postalcode >= 'AABB' AND postalcode <= 'AACC') OR (postalcode >= 'DDEE' AND postalcode <= 'DDFF'))
ORDER BY town ASC, street ASC, number ASC, number_addition ASC
LIMIT 1
This way I can get first client.
Then I want to get next client (let's say I know that my current client has ID 58 and I want to get next client in the sequence - I'll have one client that's tagged as current and I want to get next/previous) and I already know ID of first client, can you please give me a hint how to achieve this? (With the same ordering)
I found this http://www.artfulsoftware.com/infotree/queries.php#75 but I dont know how to transform these examples to the command when I need to order by multiple columns.
Thanks for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 LIMIT X,Y 的两个值,其中 X 是偏移量,Y 是行数。有关信息,请参阅此。
但是,每次您只查询一行时,这都会使您的列表排序。有不同的方法可以做到这一点。
您可以做的是获取该列表中的大部分内容,也许对您来说实用即可(比如也许是 20 个,这取决于情况)。将结果保存在程序中的数组中,并迭代这些结果。如果需要更多,只需使用偏移量再次查询即可。同样,论坛在搜索时的每个页面中仅显示一定数量的帖子。
另一种方法是获取所需的所有客户端 ID,将其保存在一个数组中,然后一次查询每个客户端 ID。
You can use the two values of LIMIT X,Y, where X is the offset and Y is the number of rows. See this for info.
That will, however, make your list order every time you query for just one row. There are different ways to do this.
What you could do is get a good portion of that list, maybe as much as is practical for you (say maybe 20, it depends). Keep the result in an array in your program, and itterate through those. If you need more, just query again with an offset. The same way a forum will show only a certain quantity of posts in each page on a search.
Another approach is to get all the client IDs you need, keep that in an array, and query for each one at a time.
这里假设
id
是该表的主键。您需要将
id
添加到选择列表中,否则您将不知道将id
放入$previous_id
中以供下一行使用。Here it is assumed that
id
is the primary key for this table.You need to add the
id
to the select list, or you will not know theid
to put in$previous_id
for the next one in line.