PHP/MYSQL 查询:获取结果上方和下方的行!

发布于 2024-10-20 04:35:46 字数 445 浏览 1 评论 0原文

我不知何故需要一种方法来实现以下目的:

用户位于某个博客页面现在我想让他能够移动:

到下一个更受欢迎的页面或到下一个不太受欢迎的页面。意思是,像这样:(

[PAGE 5] | [PAGE 3 (User is here)] | [PAGE 2]

其中第 1 页 = 最受欢迎,第 3 页 = 最不受欢迎)。我的 MySQL 表如下所示:

[ID] [VIEWS]
[1] [1000]
[2] [2560]
[3] [3200]
[4] [200]
[5] [4000]

我的问题是特定的查询。在本例中,唯一给定的变量是 ID:2。也许你可以帮我。如果您需要更多信息,请告诉我。 (我只需要两个邻居。不需要第 4 页等。)

编辑:@Trevor 不,抱歉我不能。我改变了这个例子,使它更清楚。

I somehow need a way for the following:

User is located at some blog page Now I want to give him the ability to move either:

To the next more popular page OR to the next less popular page. Means, something like this:

[PAGE 5] | [PAGE 3 (User is here)] | [PAGE 2]

(Where Page 1 = most popular, Page 3 = least popular). My MySQL Table looks like this:

[ID] [VIEWS]
[1] [1000]
[2] [2560]
[3] [3200]
[4] [200]
[5] [4000]

My problem is the specific query. The only given variable is the ID: 2 in this case. Maybe you can help me out. Just tell me if you need further informations.
(I only need the two neighbours. Page 4 etc. would not be needed.)

Edit: @Trevor No, sorry I cant. I changed the example so its more clearly.

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

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

发布评论

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

评论(4

篱下浅笙歌 2024-10-27 04:35:46

注意:根据各种评论,@mellamokb 的答案比我的好。我会删除这个答案,但由于它被接受而无法删除。

/* Next Page */
SELECT ID
    FROM YourTable
    WHERE VIEWS <= (SELECT VIEWS FROM YourTable WHERE ID = $CurrentPageId)
        AND ID < $CurrentPageId
    ORDER BY VIEWS DESC, ID DESC LIMIT 1

/* Previous Page */
SELECT ID
    FROM YourTable
    WHERE VIEWS >= (SELECT VIEWS FROM YourTable WHERE ID = $CurrentPageId)
        AND ID > $CurrentPageId
    ORDER BY VIEWS, ID LIMIT 1

NOTE: As worked out in the various comments, @mellamokb's answer is better than mine. I would delete this answer but am unable to since it was accepted.

/* Next Page */
SELECT ID
    FROM YourTable
    WHERE VIEWS <= (SELECT VIEWS FROM YourTable WHERE ID = $CurrentPageId)
        AND ID < $CurrentPageId
    ORDER BY VIEWS DESC, ID DESC LIMIT 1

/* Previous Page */
SELECT ID
    FROM YourTable
    WHERE VIEWS >= (SELECT VIEWS FROM YourTable WHERE ID = $CurrentPageId)
        AND ID > $CurrentPageId
    ORDER BY VIEWS, ID LIMIT 1
骄兵必败 2024-10-27 04:35:46

上一页:

  select ID as PreviousId
    from PageViews
   where Views > (select Views From PageViews Where ID = @Id)
      or (
             Views = (select Views From PageViews Where ID = @Id)
             and ID > @id
         )
order by Views ASC, ID DESC
   limit 1

下一页:

  select ID as PreviousId
    from PageViews
   where Views < (select Views From PageViews Where ID = @Id)
      or (
             Views = (select Views From PageViews Where ID = @Id)
             and ID < @id
         )
order by Views DESC, ID DESC
   limit 1

For previous page:

  select ID as PreviousId
    from PageViews
   where Views > (select Views From PageViews Where ID = @Id)
      or (
             Views = (select Views From PageViews Where ID = @Id)
             and ID > @id
         )
order by Views ASC, ID DESC
   limit 1

For next page:

  select ID as PreviousId
    from PageViews
   where Views < (select Views From PageViews Where ID = @Id)
      or (
             Views = (select Views From PageViews Where ID = @Id)
             and ID < @id
         )
order by Views DESC, ID DESC
   limit 1
月竹挽风 2024-10-27 04:35:46

或者

    /* Previous Page */
SELECT ID
    FROM YourTable
    WHERE VIEWS < $CurrentPageViews 
    ORDER BY VIEWS DESC LIMIT 1

/* Next Page */
SELECT ID
    FROM YourTable
    WHERE VIEWS > $CurrentPageViews
    ORDER BY VIEWS LIMIT 1

or

    /* Previous Page */
SELECT ID
    FROM YourTable
    WHERE VIEWS < $CurrentPageViews 
    ORDER BY VIEWS DESC LIMIT 1

/* Next Page */
SELECT ID
    FROM YourTable
    WHERE VIEWS > $CurrentPageViews
    ORDER BY VIEWS LIMIT 1
电影里的梦 2024-10-27 04:35:46

三个查询:

SELECT @MyID:=ID,@MyViews:=Views FROM Table WHERE ID=2;

使用本地“视图”:

SELECT ID,Views FROM Table WHERE Views <= @MyViews AND ID != @MyID ORDER BY Views DESC LIMIT 1;
SELECT ID,Views FROM Table WHERE Views >= @MyViews AND ID != @MyID ORDER BY Views ASC LIMIT 1;

并且,确保您在视图上有索引。

Three queries:

SELECT @MyID:=ID,@MyViews:=Views FROM Table WHERE ID=2;

Using local "Views":

SELECT ID,Views FROM Table WHERE Views <= @MyViews AND ID != @MyID ORDER BY Views DESC LIMIT 1;
SELECT ID,Views FROM Table WHERE Views >= @MyViews AND ID != @MyID ORDER BY Views ASC LIMIT 1;

And, make sure you have an index on Views.

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