sql循环并设置属性

发布于 2024-10-16 06:24:16 字数 658 浏览 3 评论 0原文

在SQL中,我想查询一个列表,按pageNumber排序

SELECT * FROM `comics` 
WHERE 1
ORDER BY pageNumber ASC

然后,我想根据查询中的索引设置它们的pageNumbers(从1而不是0开始)。

这是我所需功能的伪代码;其中list是上面Select Query的返回值。

for(var n:int = 0; n<list.length; n++){
    if(list[n].pageNumber != n+1){
        list[n].pageNumber = n+1
    }
}

例如,我可能有 pageNumbers 5, 17, 23, 24, 18, 7

ORDER BY pageNumber ASC 会将其排序为 5, 7, 17, 18, 23, 24 >

然后我想将页码更改为 1, 2, 3, 4, 5, 6

编辑:

@fortheworld MySQL

@cyberkiwi UPDATE

很抱歉不清楚。我想我需要了解更多才能让我的问题清楚:) 感谢你的帮助

In SQL, I would like to query a list, in order by pageNumber

SELECT * FROM `comics` 
WHERE 1
ORDER BY pageNumber ASC

Then, I would like to set their pageNumbers based on their index in the query (starting with 1 instead of 0).

Here is my pseudo code for the functionality as desired; where list is the return value of the Select Query above.

for(var n:int = 0; n<list.length; n++){
    if(list[n].pageNumber != n+1){
        list[n].pageNumber = n+1
    }
}

For example I might have pageNumbers 5, 17, 23, 24, 18, 7

The ORDER BY pageNumber ASC will sort this to 5, 7, 17, 18, 23, 24

I would then like to alter the pageNumbers in order to be 1, 2, 3, 4, 5, 6

edit:

@fortheworld MySQL

@cyberkiwi UPDATE

sorry for being unclear. guess i need to learn more for my questions to be clear :)
thanks for all your help

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

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

发布评论

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

评论(2

半葬歌 2024-10-23 06:24:16
SET @I := 0;

  SELECT *,
         @I := @I + 1 AS newPageNumber
    FROM comics
ORDER BY pageNumber ASC
SET @I := 0;

  SELECT *,
         @I := @I + 1 AS newPageNumber
    FROM comics
ORDER BY pageNumber ASC
江挽川 2024-10-23 06:24:16

我不明白为什么人们坚持编写一条 SQL 批处理,而一条语句就可以了。

SELECT comics.*, @n := @n + 1 AS PageNumber2
FROM (SELECT @n := 0) X CROSS JOIN comics
ORDER BY pageNumber ASC

I don't understand why peops insist on writing an SQL batch when a single statement will do.

SELECT comics.*, @n := @n + 1 AS PageNumber2
FROM (SELECT @n := 0) X CROSS JOIN comics
ORDER BY pageNumber ASC
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文