如何以最少的提取记录按字母顺序获取下一条和上一条记录?

发布于 2024-08-23 17:44:16 字数 181 浏览 7 评论 0原文

我有一个页面显示表 A 中的公司名称及其详细信息。 现在假设我显示了一家公司,其名称为“Company_one”,现在我想按字母顺序排序下一家公司和上一家公司及其详细信息等。

我的表中的数据未排序。它在获取数据时存储。

那么现在我应该写什么样的查询,它只给出一个前一个和一个下一个按字母顺序排序的记录? 请帮忙!!

I have a page that is displaying a company name and its details from a table A.
Now say i have a company displayed,and its name is 'Company_one' now i want to have alphabetically sorted next company and previous company and their details etc.

The data in my table is not sorted.Its stored as it gets the data.

So now what kind of query should i write that it gives only one previous and one next alphabetically sorted record??
Plz help!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

纵性 2024-08-30 17:44:16

没有好的方法可以在单个查询中做到这一点。只需执行两个查询即可。

要获取上一个:

SELECT * FROM companies
WHERE name < variable_with_current_name
ORDER BY name DESC
LIMIT 1

要获取下一个:

SELECT * FROM companies
WHERE name > variable_with_current_name
ORDER BY name ASC
LIMIT 1

There's no nice way to do that in a single query. Just do two queries.

To get the previous one:

SELECT * FROM companies
WHERE name < variable_with_current_name
ORDER BY name DESC
LIMIT 1

To get the next one along:

SELECT * FROM companies
WHERE name > variable_with_current_name
ORDER BY name ASC
LIMIT 1
ζ澈沫 2024-08-30 17:44:16

您需要使用排序子句 对表格进行排序。 sort 的原型是:

sort by fieldname

示例查询:

select * from your_table sort by company asc

如果要限制记录,请使用 limit 子句:

select * from your_table sort by company asc limit 0, 1

You need to use the sort clause to sort your table. The prototype for sort is:

sort by fieldname

Example Query:

select * from your_table sort by company asc

If you want to limit records, use limit clause:

select * from your_table sort by company asc limit 0, 1
十级心震 2024-08-30 17:44:16

根据 Dominic 的回答,您可以通过将它们与 WHEREOR 组合使用单个查询来获得相同的结果。

例如:

SELECT * FROM `companies`
WHERE (
    `name` = IFNULL(
        (SELECT `name` FROM `companies` 
            WHERE `name` < 'variable_with_current_name'
            ORDER BY `name` DESC
            LIMIT 1)
        , 0)
OR
    `name` = IFNULL(
        (SELECT `name` FROM `companies` 
            WHERE `name` > 'variable_with_current_name'
            ORDER BY `name` ASC
            LIMIT 1)
        , 0)
) 

希望有帮助。

Based on Dominic's answer, you can achieve the same result using a single query by combining them with WHERE and OR.

For example:

SELECT * FROM `companies`
WHERE (
    `name` = IFNULL(
        (SELECT `name` FROM `companies` 
            WHERE `name` < 'variable_with_current_name'
            ORDER BY `name` DESC
            LIMIT 1)
        , 0)
OR
    `name` = IFNULL(
        (SELECT `name` FROM `companies` 
            WHERE `name` > 'variable_with_current_name'
            ORDER BY `name` ASC
            LIMIT 1)
        , 0)
) 

Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文